1

以下是 1 到 M 型号:

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

class Upload < ActiveRecord::Base
    belongs_to :foto_gossip
end

现在我想要 Uploads.all 的条件为 :published_at NOT NULL 的相应上传的父模型?

4

1 回答 1

7

只需将其添加到您的上传模型:

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

然后您可以像这样使用已发布的 foto_gossip 获取所有上传:

Upload.with_published_foto_gossip 
于 2009-11-06T13:50:00.290 回答