Marnen Laibow-Koser wrote:
> class NodeMembership < AR::B # join model since Rails won't do
> polymorphic habtm
> belongs_to :node, :polymorphic => true
> belongs_to :relationship
> end
>
> class Relationship < AR::B
> has_many :node_memberships
> has_many :nodes, :through => :node_memberships # may also need
> :polymorphic => true -- not sure
> end
>
> class Model[A,B,C...] < AR::B
> # you will probably want to refactor this into an abstract base class
> has_many :node_memberships, :as => :node
> has_many :relationships, :through => :node_memberships
> end
What is the best way to get a list of related nodes using these models?
For example, I want to find all nodes that have a relation to a single
node. The only way I can see to do this is:
@obj = ModelA.find(1, :include => 'relationships)
@rels = @obj.relationships
@rels.each do |r|
r.nodes.each do |n|
if n.node_type != 'ModelA' && n.node_id != @obj.id
if n.node_type == 'ModelA'
nodes.push(ModelA.find(n.node_id)
end
end
end
end
This seems horribly complicated and I think there would be a lot of SQL
calls making this massively inefficient. Is there a better way?
Thanks.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---