0
有文章和評論。評論屬於文章。我們的目標是讓所有評論發佈在發佈的文章上。根據其他模型的條件獲取記錄
class Article < ActiveRecord::Base
has_many :comments
# published
def self.is_published
where(published: true)
end
。
class Comment < ActiveRecord::Base
belongs_to :article
def method_name
# get all comments where article.is_published
end
end
我們使用的控制器:
@comments = Comment.method_name.order("created_at desc")