#3
Mar 08, 2007

Find Through Association

No need to pass foreign keys in find conditions, just do the find through a has_many association.
Download (5.2 MB, 2:17)
alternative download for iPod & Apple TV (3.2 MB, 2:17)
# projects_controller.rb
def show
  @project = Project.find(params[:id])
  @tasks = @project.tasks.find_all_by_complete(false)
end

RSS Feed for Episode Comments 11 comments

1. Klaus Paiva Jul 18, 2007 at 04:53

Nice examples! All these tips will certainly contribute to a clever and beautiful code. Thanks!


2. bob Aug 07, 2007 at 08:15

will this work if you have @projects, i.e multiple projects?


3. Ryan Bates Aug 07, 2007 at 09:32

@bob, nope. If you have an array of projects you'll have to fetch the tasks for all those projects using a custom find of some sort.


4. weelkoh0 Sep 08, 2007 at 04:28

Hi Ryan,

I have been checking your website for more has_many :through articles.

I have this question which I hope you could provide your insights, as I have been checking around but to-date still could not fully understand.

I would greatly appreciate if you could look at my code on http://railsforum.com/viewtopic.php?id=10204

I have these 2 tables: groups and users, where a group can have many users and an user can have many groups.

I have this third table, roles, which contains group_id, user_id, type. The field 'type' can be Administrator, Owner and Member. A user can play the role of Owner for a group but assume the role of member for another group. I use this table as Single Table Inheritance.

I want to do such query:
Based on a given user's id, I want to
display all the groups of this user, with the respective role for that group.

I have been reading and trying the associations. The last working association I tried is (assuming the user-id I want to check is 1):

User.find(1).roles.each do |role|
  <%= role.group.name %>
end

Is that the way to perform such query?

Thank you, for your great effort in sharing your knowledge with others.

Thank you very much.


5. Yui-Ikari Nov 06, 2007 at 11:17

I have the same problem as weelkohO
would be nice to have a railcast with that specific problem ^___^
pls! ^_^


6. kino May 23, 2008 at 01:54

By immersing ourselves meditatively in the general intentions of modes of consciousness, we discover that noematic descriptions are "in themselves".


7. Jim Jul 04, 2008 at 23:05

Is this really a Find Through Association or a has_many / belongs_to.

What about a many to many 'find through' association?


8. fgd May 02, 2009 at 22:42

<a href="http://www.m3m7.com" rel="nofollow">شات</a><br />
<a href="http://www.x333x.com" rel="nofollow">شات</a><br />


9. tiffany rings May 06, 2009 at 19:24

will this work if you have @projects, i.e multiple projects?


10. tiffany-rings-c-69.html May 06, 2009 at 22:59

Nice examples!


11. Eric May 13, 2009 at 09:56

Great example. Just what I was looking for. Thanks!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player