在哪里可以找到has_many的关系方法?

时间:2012-04-02 07:44:53

标签: ruby-on-rails ruby

class Project
  has_many :pages
end

class Page
  belongs_to :project
end


@project = Project.first
@project.pages.list_out

我应该在哪里设置一个方法list_out用于页面?

2 个答案:

答案 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