In your example you have defined in Item model relationship as has_many for collection_items and collections the generated association method is collection_items and collections respectively both of them returns an array so the way you are trying to access here is wrong. This is primarily case of mant to many relationship. Just check this Asscociation Documentation for further reference.
Thanks for the useful link. And for pointing out that "item. Collection_item.
Whatever" is obviously wrong. I'll amend my question to reflect that. The gist of my questions is: (how) can I get at the join table attributes via the coll.
Items array? – Noel Walters Mar 12 '09 at 10:54.
Do_something_with( item. Collection_item_id ) This fails because item does not have a collection_item_id member. Do_something_with( item.
Collection_item. Collection_item_id ) This fails because item does not have a collection_item member. Remember that the relation between item and collection_items is a has_many.
So item has collection_items, not just a single item. Also, each collection has a list of collection items. What you want to do is probably this: colls = Collection.
Find :all colls. Each do |coll| coll. Collection_items.
Each do |collection_item| do_something_with( collection_item. Id ) end end A couple of other pieces of advice: Have you read the documentation for has_many :through in the Rails Guides? It is pretty good.
You shouldn't need the :source parameters in the has_many declarations, since you have named your models and associations in a sensible way. I found from online documentation that using has_and_belongs_to_many will attach the join table attributes to the retreived items, but apparently it is deprecated. I haven't tried it yet.
I recommend you stick with has_many :through, because has_and_belongs_to_many is more confusing and doesn't offer any real benefits.
Thanks - If I could accept two answers I would - I did read the documentation for has_many :through and now that I know the answer it seems obvious - too many late nights! – Noel Walters Mar 12 '09 at 12:08.
I was able to get this working for one of my models: class Group :memberships, :source => :user do def with_join proxy_target. Map do |user| proxy_owner = proxy_owner() user.metaclass. Send(:define_method, :membership) do memberships.
Detect {|_| _. Group == proxy_owner} end user end end end end In your case, something like this should work (haven't tested): class Collection :position has_many :items, :through => :collection_items, :source => :item, :order => :position do def with_join proxy_target. Map do |items| proxy_owner = proxy_owner() item.metaclass.
Send(:define_method, :join) do collection_items. Detect {|_| _. Collection == proxy_owner} end item end end end end Now you should be able to access the CollectionItem from an Item as long as you access your items like this (items.
With_join): def helper_method( collection_id ) colls = Collection. Find :all colls. Each do |coll| coll.items.
With_join. Each do |item| do_something_with( item.join. Collection_item_id ) end end end Here is a more general solution that you can use to add this behavior to any has_many :through association: http://github.com/TylerRick/has_many_through_with_join_model class Collection :position has_many :items, :through => :collection_items, :source => :item, :order => :position, :extend => WithJoinModel end.
I want to use the collection_item_id to keep track of the currently selected item between requests, but I can't access any attributes of the join model via the Item class. But they also fail. Can anyone advise me how to proceed with this?
I found from online documentation that using has_and_belongs_to_many will attach the join table attributes to the retreived items, but apparently it is deprecated. I haven't tried it yet. And changing the helper to use coll.
Collection_items instead of coll. It's made a mess of my code - because of other factors not detailed here - but nothing that an hour or two of re-factoring won't sort out.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.