Skip to content

Commit

Permalink
Rails 4 compat: port named scopes to new syntax
Browse files Browse the repository at this point in the history
Subtle edge case bugs pop up in AR 4 when named scopes are declared with
the old syntax. This is likely a bug with AR, but work around it for now.
  • Loading branch information
mislav committed Sep 16, 2013
1 parent 56c7ad5 commit 8a0ead9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion spec/fixtures/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def self.with_poor_ones(&block)
end
end

scope :poor, :conditions => ['salary <= ?', 80000], :order => 'salary'
scope :poor, lambda {
where(['salary <= ?', 80000]).order('salary')
}

def self.per_page() 10 end
end
7 changes: 4 additions & 3 deletions spec/fixtures/reply.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Reply < ActiveRecord::Base
belongs_to :topic, :include => [:replies]

scope :recent,
:conditions => ['replies.created_at > ?', 15.minutes.ago],
:order => 'replies.created_at DESC'
scope :recent, lambda {
where(['replies.created_at > ?', 15.minutes.ago]).
order('replies.created_at DESC')
}

validates_presence_of :content
end
8 changes: 6 additions & 2 deletions spec/fixtures/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class Topic < ActiveRecord::Base
has_many :replies, :dependent => :destroy, :order => 'replies.created_at DESC'
belongs_to :project

scope :mentions_activerecord, :conditions => ['topics.title LIKE ?', '%ActiveRecord%']
scope :distinct, :select => "DISTINCT #{table_name}.*"
scope :mentions_activerecord, lambda {
where(['topics.title LIKE ?', '%ActiveRecord%'])
}
scope :distinct, lambda {
select("DISTINCT #{table_name}.*")
}
end

0 comments on commit 8a0ead9

Please sign in to comment.