Toby Rodwell wrote:
> I have a 'Circuit' table, and a 'Glue' table to perform a many-to-many
> join on the circuit records, such at a circuit can have many subcircuits
> (its component parts) and many supercircuits (circuits which it itself
> is a part of).
In other words, you have a hierarchical tree of circuits.
>
> has_and_belongs_to_many :subcircuits,
> :class_name => "Circuit",
> :join_table => "circuit_glue",
> :association_foreign_key => "PTR_component",
> :foreign_key => "PTR_circuit"
> has_and_belongs_to_many :supercircuits,
> :class_name => "Circuit",
> :join_table => "circuit_glue",
> :foreign_key => "PTR_component",
> :association_foreign_key => "PTR_circuit"
>
> With this, I can do successfully things like
> my_circuit.subcircuits[0].name
> but if I try
> my_circuit.subcircuits[0].subcircuits.any_method
> ... I get a NoMethodError: You have a nil object when you didn't expect
> it!
> The objects in the cct.subciruits array do not appear to 'full' circuit
> objects in as much as they do not repsonsd to 'subcircuits'
> To build a recursive list of subcircuits I find I have to resort to
>
> self.subcircuits.each { |sc|
> subcirc_array.push(Circuit.find(:all).detect { |c| c.absid.to_s ==
> sc.PTR_component })}
> ... which seems pretty slow and laborious. Any advice on how to speed
> this up, and/or get ...subciruits[n].subcircuits to work as required?
> Thanks in advance.
You want awesome_nested_set, which will let you do that with one query.
The Glue model is unnecessary.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
--
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.