I’m new to Ruby, Rails and this group, so first I’d like to say is
hello and thanks for this awesome language, framework, and community.
I have a graph linked by an edge model using has_many :through like the
one described here:
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through
Users habtm Links (directed edges), and vice versa. I have a table
links_users. So far this all works fine.
The problem is, I want to be able to say, get all children (neighbors
reachable in 1 step from this node) only through links that belong to
a given user (typically, the current user).
I wrote some methods on Node like this:
def links_as_parent_for(user)
Link.find(:all,
:joins => “INNER JOIN links_users ON links_users.link_id =
links.id”,
:conditions => "links_users.user_id = " + user.id.to_s +
" AND links.parent_id = " + self.id.to_s)
end
def children_for(user)
links_as_parent_for(user).collect { |l| l.child }
end
This works, but it would be great if this could all act as an
association so that, for example, if the objects were already in memory
it wouldn’t issue an additional SQL query (plus it would be nice to say
node.children and get the right results). I don’t think that
associations can take arguments, but can they check the current user
some other way?
Is this possible? Any suggestions?
Thank you for your responses,
Nick Urban