#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 1 comment

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. 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?


7. Eric May 13, 2009 at 09:56

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


8. Grant Sep 21, 2009 at 21:55

Thank you for this Railscast. Your work is slowly helping me understand the more interesting side of rails.
The on issue I felt would have been nice here would be how to use the @accounts in your view. This would have complete the information needed to create the HABTM. I.E. if we had a user with an account and wanted to pull up the account for the user as a view, how would one use this?

Thanks Again.


9. Linka Mar 30, 2010 at 03:06

thanks, great example.


10. Dimusya Mar 31, 2010 at 15:22

thanks-you are the best...


11. Christian Louboutin Sandals Jun 24, 2010 at 02:22

Thank you very much!
Your article is very useful!


12. Gary Jun 27, 2010 at 19:16

A four year old little boy was at the doctor's office with his mother in the waiting room when he spotted a pregnant lady on the other side of the room. Having nothing better to do, he walks over to her and inquisitively asks "Why is your stomach so big?"


13. sample post Jun 30, 2010 at 04:02

is old though.


14. free directory list Aug 11, 2010 at 22:02

good one site luv that stuff


15. free directory list Aug 11, 2010 at 22:02

good one site luv that stuff


16. free directory list Aug 11, 2010 at 22:02

good one site luv that stuff


17. p90 workout Aug 12, 2010 at 09:13

Although I can't agree with all you staded, I must confess I do love your way of writing.


18. uggs online Aug 13, 2010 at 22:08

Article is very nicely written.


19. cheap air jordans Aug 14, 2010 at 01:25

The on issue I felt would have been nice here would be how to use the @accounts in your view. This would have complete the information needed to create the HABTM. I.E. if we had a user with an account and wanted to pull up the account for the user as a view. Great example. Just what I was looking for. Thanks!


20. selma Aug 19, 2010 at 13:44

thanks admin


21. selma Aug 19, 2010 at 13:45

thanks admin


22. louis vuitton shoes Aug 26, 2010 at 20:56

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


23. snow boots Aug 31, 2010 at 02:16

Your work is slowly helping me understand the more interesting side of rails.


24. louis vuitton sunglasses Sep 01, 2010 at 21:30

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

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
Give Back to Open Source