开发者

Rails Filter records of child model based upon the parent model attribute

Following are the 1-to-M models:

class FotoGossip < ActiveRecord::Base
    has_many :uploads
    attr_accessible :published_at, ...
end 

class Upload < ActiveRecord::Base
    belongs_to :foto_gossip
end

Now I want the Uplo开发者_开发百科ads.all with the condition :published_at NOT NULL of the corresponding upload's parent model?


Just add this to your Upload model:

named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL"

then you can get all the uploads with published foto_gossip like this:

Upload.with_published_foto_gossip 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜