Matthew Shapiro wrote:
> Marnen Laibow-Koser wrote:
>> I think it is -- even just replacing relationships with node_memberships
>> would help a bit. But the generated SQL will still be somewhat
>> inefficient. I'm trying to come up with a way to fix it and keep the
>> polymorphism.
>
> I don't see how going doing a @node.node_memberships will help, because
> that seems to only give me the node_membership records that @node is in
> (meaning I would then have to get the relationship_id value from that
> and then .find it). Unless I am missing something which is entirely
> possible.
No, I think you're right. I apparently wasn't able to juggle complex
associations in my head as well as I thought.
>
> However, it turns out that the relationships aren't working how you set
> them up, even with the nested_has_many plugin. Look at the following
> output from ruby's console:
>
>>> ModelA.find(1).node_memberships.count
> ModelA.find(1).node_memberships.count
> => 1
>>> ModelA.find(1).relationships.count
> ModelA.find(1).relationships.count
> => 0
>>> ModelA.find(1).node_memberships[0]
> ModelA.find(1).node_memberships[0]
> => #<NodeMembership id: 8, node_id: 1, node_type: "ModelA",
> relationship_id: 60, created_at: "2009-10-27 01:03:56", updated_at:
> "2009-10-27 01:03:56">
>>> Relationship.find(60)
> Relationship.find(60)
> => #<Relationship id: 60, name: nil, description: nil, created_at:
> "2009-10-27 00:35:11", updated_at: "2009-10-27 00:35:11">
>>> NodeMembership.find(5).relationship
> NodeMembership.find(5).relationship
> => #<Relationship id: 60, name: nil, description: nil, created_at:
> "2009-10-27 00:35:11", updated_at: "2009-10-27 00:35:11">
>>> Relationship.find(60).nodes
> Relationship.find(60).nodes
> ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a
> has_many :through association 'Relationship#nodes' on the polymorphic
> object 'Node#node'.
> from
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/reflection.rb:297:in
>
> `check_validity_without_nested_has_many_through!'
> from
> C:/Users/KallDrexx/Documents/Scrawl/scrawl/vendor/plugins/nested_has_many_through/lib/nested_has_many_through.rb:8:in
>
> `check_validity!'
> from
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/has_many_through_association.rb:5:in
>
> `initialize'
> from
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations.rb:1297:in
>
> `new'
> from
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations.rb:1297:in
>
> `nodes'
> from (irb):11
>
> -----------------------------------------------------------
>
> To summarize, the database has the correct records in the
> node_memberships and relationships table, yet for some reason rails
> can't connect from relationships to ModelA and vice-versa. Does
> anything pop out at you on why?
Not immediately. I'll look closer.
>
>> Have you read Programming Ruby yet?
>
> No, I learned ruby via tutorials found on google, and most of them
> didn't go in depth into the array methods unfortunately.
Read Programming Ruby (there's a free Web edition). It's really a
prerequisite for doing anything serious with the language.
>
>> &:symbol is the same as :symbol.to_proc. This in turn is defined in
>> such a way that array.collect(&:fn) is equivalent to (but slightly
>> slower than) array.collect{|x| x.fn} .
>
> Thanks for that explanation, that made sense.
>
> Thanks again for all your help by the way.
You're welcome!
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
-~----------~----~----~----~------~----~------~--~---