DEV: prefer using ordering in relation over default scope

This commit is contained in:
Sam 2018-05-29 09:34:12 +10:00
parent 16d0ab5654
commit df815d6c0e
3 changed files with 8 additions and 3 deletions

View File

@ -48,7 +48,7 @@ class Post < ActiveRecord::Base
has_many :post_details has_many :post_details
has_many :post_revisions has_many :post_revisions
has_many :revisions, foreign_key: :post_id, class_name: 'PostRevision' has_many :revisions, -> { order(:number) }, foreign_key: :post_id, class_name: 'PostRevision'
has_many :user_actions, foreign_key: :target_post_id has_many :user_actions, foreign_key: :target_post_id

View File

@ -4,8 +4,6 @@ class PostRevision < ActiveRecord::Base
belongs_to :post belongs_to :post
belongs_to :user belongs_to :user
default_scope { order('number ASC') }
serialize :modifications, Hash serialize :modifications, Hash
after_create :create_notification after_create :create_notification

View File

@ -1095,4 +1095,11 @@ describe Post do
expect(hidden_topic.visible).to eq(false) expect(hidden_topic.visible).to eq(false)
end end
it "automatically orders post revisions by number ascending" do
post = Fabricate(:post)
post.revisions.create!(user_id: 1, post_id: post.id, number: 2)
post.revisions.create!(user_id: 1, post_id: post.id, number: 1)
expect(post.revisions.pluck(:number)).to eq([1, 2])
end
end end