Permalink
Browse files

Rails 4 compat: port named scopes to new syntax

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...
1 parent c5abe4d commit 4557845df677f7cede52774ce8a645d7ce604814 @mislav committed Jun 21, 2013
Showing with 13 additions and 6 deletions.
  1. +3 −1 spec/fixtures/developer.rb
  2. +4 −3 spec/fixtures/reply.rb
  3. +6 −2 spec/fixtures/topic.rb
@@ -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
View
@@ -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
View
@@ -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 4557845

Please sign in to comment.