ruby on rails - How to query across three models -
i have workspace, project, user, , membership models. user has many memberships, , many projects through memberships. project belongs work space.
getting users projects pretty easy:
user.projects
but reaching across work spaces tricky. raises error of undefined method spaces collection proxy
.
user.projects.work_spaces.unique
how can unique set of work spaces user involved in? (work spaces projects user belongs through memberships).
if using activerecord, suggest leveraging association methods in order avoid verbose code , poorer performance of .collect(&:work_spaces).flatten.uniq.
class user < activerecord::base has_many :memberships has_many :projects, through: :memberships has_many :work_spaces, through: :projects end
you able ask user.work_spaces
.
Comments
Post a Comment