#3
Mar 09
Find Through Association
No need to pass foreign keys in find conditions, just do the find through a has_many association.
# projects_controller.rb def show @project = Project.find(params[:id]) @tasks = @project.tasks.find_all_by_complete(false) end




Nice examples! All these tips will certainly contribute to a clever and beautiful code. Thanks!
will this work if you have @projects, i.e multiple projects?
@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.
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.
I have the same problem as weelkohO
would be nice to have a railcast with that specific problem ^___^
pls! ^_^