Hi, my first question, and probably a newbish one:
So I have a projects model, which belongs to a user :through an
assignment table...
like this:
class User
has_many :assignments
has_many :projects, :through => :assignments
end
class Project
has_many :assignments
has_many :users, :through => :assignments
end
class Assignment
belongs_to :user
belongs_to :project
end
Now I want in the startpage (home controller), after logging in, see
all the projects assigned to the current user.
How do I wwrite that query?
I have assignments like
@assignments = Assignment.where("user_id = ?", current_user.id)
but if I try the same thing with projects I get an error.... as there
is no user_id column in projects..
there must be an elegant way to query this with Active Record, right?
thanks in advance
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jan-31 14:57 UTC
Re: find objects in many_to_many (:through) relation
On Jan 31, 9:09 am, frankblizzard <tmaxim...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> @assignments = Assignment.where("user_id = ?", current_user.id) > > but if I try the same thing with projects I get an error.... as there > is no user_id column in projects.. > > there must be an elegant way to query this with Active Record, right? >You wouldn''t normally use where (or find in previous versions of rails) for this sort of stuff. Setting up associations with has_many etc. means you can do current_user.assignments, current_user.projects etc. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.