Skip to content

Commit a519845

Browse files
committed
Updates are never updated!
1 parent bb03be6 commit a519845

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

app/models/project.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Project < ApplicationRecord
2121
has_many :project_interests
2222
has_many :keywords, through: :project_interests
2323

24-
has_many :updates, dependent: :destroy
24+
has_many :updates, -> { order 'created_at DESC' }, dependent: :destroy
2525

2626
has_many :comments, as: :commentable, dependent: :destroy
2727

app/models/user.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class User < ApplicationRecord
77
validates_uniqueness_of :email
88

99
has_many :originated_projects, foreign_key: 'originator_id', class_name: 'Project'
10-
has_many :updates, -> { order 'updated_at DESC' }, foreign_key: 'author_id', dependent: :destroy
10+
has_many :updates, -> { order 'created_at DESC' }, foreign_key: 'author_id', dependent: :destroy
1111

1212
has_many :memberships
1313
has_many :comments, foreign_key: 'commenter_id'

spec/models/project_spec.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
before do
6565
@project = FactoryBot.create(:project)
6666
@idea = FactoryBot.create(:idea)
67+
# We sort updates by updated_at, make sure it's different from the creation updates
68+
sleep 1
6769
@user = FactoryBot.create(:user)
6870
@project.join!(@user)
6971
@idea.join!(@user)
@@ -78,21 +80,23 @@
7880
end
7981

8082
it 'creates an Update object assigned to the user' do
81-
expect(@project.updates.last.author).to eq(@user)
83+
expect(@project.updates.first.author).to eq(@user)
8284
end
8385

8486
context 'when project has no users (state == idea)' do
85-
it { expect(@idea.updates.last.text).to eq('started') }
87+
it { expect(@idea.updates.first.text).to eq('started') }
8688
end
8789

8890
context 'when project has a user (state == project)' do
89-
it { expect(@project.updates.last.text).to eq('joined') }
91+
it { expect(@project.updates.first.text).to eq('joined') }
9092
end
9193
end
9294

9395
describe 'leave!' do
9496
before do
9597
@project = FactoryBot.create(:project)
98+
# We sort updates by updated_at, make sure it's different from the creation updates
99+
sleep 1
96100
@user = @project.users.first
97101
@project.leave!(@user)
98102
end
@@ -102,8 +106,8 @@
102106
end
103107

104108
it 'creates an Update for the user with text "left"' do
105-
expect(@project.updates.last.author).to eq(@user)
106-
expect(@project.updates.last.text).to eq('left')
109+
expect(@project.updates.first.author).to eq(@user)
110+
expect(@project.updates.first.text).to eq('left')
107111
end
108112

109113
context 'when the removed user was the last one' do

0 commit comments

Comments
 (0)