有人能看看我做错了什么吗?是Rails 3:
class Ad < ActiveRecord::Base
belongs_to :postingtemplate
scope :active, (lambda do |ad|
Item.exists?(:postingtemplate => ad.postingtemplate_id)
end)
end它是Ad模型中的一个作用域,并且应该返回所有的广告,哪个项目存在于item.postingtemplate == ad.postingtemplate_id中
更新
)将其分解为两个作用域,它起了作用:)
class Ad < ActiveRecord::Base
belongs_to :postingtemplate
scope :active, where(:postingtemplate_id => Postingtemplate.active)
end
class Postingtemplate < ActiveRecord::Base
has_many :ads
scope :active, where(:id => Item.all.collect{|x| x.postingtemplate}.uniq)
end如果有人知道更好的方法-随便告诉我
发布于 2013-04-13 14:58:47
你可以用join来做
scope :active, lambda { |ad| joins(:postingtemplate => :items).where(:postingtemplate => {:postingtemplate_id => ad.postingtemplate_id}) }也许这也会奏效:
scope :active, lambda { |ad| joins(:postingtemplate => :items).where(:postingtemplate => ad.postingtemplate) }https://stackoverflow.com/questions/15988893
复制相似问题