【发布时间】:2013-12-12 23:15:57
【问题描述】:
在我的 Rails 应用程序中,people 可以有多个 projects,反之亦然:
# app/models/person.rb
class Person < ActiveRecord::Base
has_many :people_projects
has_many :projects, :through => :people_projects
end
# app/models/people_project.rb
class PeopleProject < ActiveRecord::Base
belongs_to :person
belongs_to :project
end
# app/models/project.rb
class Project < ActiveRecord::Base
has_many :people_projects
has_many :people, :through => :people_projects
def self.search(person_id)
if person_id
where("person_id = ?", person_id) # not working because no person_id column in projects table
else
scoped
end
end
end
如何在我的ProjectsController 的index 视图中按person_id 过滤项目,例如通过使用这样的 URL:http://localhost:3000/projects?person_id=164
我无法理解这一点。请帮忙!谢谢...
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord associations