class Project
has_many :pages
end
class Page
belongs_to :project
end
@project = Project.first
@project.pages.list_out
我应该在哪里设置一个方法list_out用于页面?
答案 0 :(得分:4)
class Project
has_many :pages
scope :list_out, joins(:pages).where('pages.project_id = ?', self.id)
end
class Page
belongs_to :project
end
@project = Project.first
@project.list_out
答案 1 :(得分:1)
class Project
has_many :pages
def list_out
pages.map(&:id)
end
end
class Page
belongs_to :project
end
@project = Project.first
@project.list_out