开发者

Rails 3 plugin scope_out doesn't work

开发者 https://www.devze.com 2023-01-29 06:59 出处:网络
Hey, I\'m tried to install the scope_out plugin through \"rails plugin install http://scope-out-rails.googlecode.com/svn/trunk/\"

Hey, I'm tried to install the scope_out plugin through "rails plugin install http://scope-out-rails.googlecode.com/svn/trunk/"

and added scope_out to a model of mine:

class MessageCopy < ActiveRecord::Base
  belongs_t开发者_Go百科o :message
  belongs_to :recipient, :class_name => "User"
  belongs_to :folder
  delegate   :author, :created_at, :subject, :body, :recipients, :to => :message
  scope_out  :deleted
  scope_out  :not_deleted, :conditions => ["deleted IS NULL OR deleted = ?", false]
end

but I always get the error: undefined method `scope_out'


The scope_out plugin is not compatible with Rails3.

Judging from the explanation of the plugin, it may very well be obsolete now. Instead, you can write those scopes as:

class MessageCopy < ActiveRecord::Base
  ...
  scope :deleted, where(:deleted => true)
  scope :not_deleted, where("deleted IS NULL OR deleted = ?", false)
end

More information about the new ActiveRelation syntax here.

You can write this using raw ARel objects as well:

where( arel_table[:deleted].eq(nil).or( arel_table[:deleted].eq(false) ) )

I've been using a new gem called MetaWhere to augment standard ARel. It would let you write not_deleted as:

scope :not_deleted, where({ :deleted => nil } | { :deleted => false })

If you're using Rails 3.1, try out the successor to MetaWhere called Squeel

scope :not_deleted, where{ ( deleted == nil ) | ( deleted == false ) }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号