Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to access rails join model attributes when using has_many :through
    text
    copied!<p>I have a data model something like this:</p> <pre><code># columns include collection_item_id, collection_id, item_id, position, etc class CollectionItem &lt; ActiveRecord::Base self.primary_key = 'collection_item_id' belongs_to :collection belongs_to :item end class Item &lt; ActiveRecord::Base has_many :collection_items has_many :collections, :through =&gt; :collection_items, :source =&gt; :collection end class Collection &lt; ActiveRecord::Base has_many :collection_items, :order =&gt; :position has_many :items, :through =&gt; :collection_items, :source =&gt; :item, :order =&gt; :position end </code></pre> <p>An Item can appear in multiple collections and also more than once in the same collection at different positions.</p> <p>I'm trying to create a helper method that creates a menu containing every item in every collection. 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.</p> <pre><code>def helper_method( collection_id ) colls = Collection.find :all colls.each do |coll| coll.items.each do |item| # !!! FAILS HERE ( undefined method `collection_item_id' ) do_something_with( item.collection_item_id ) end end end </code></pre> <p>I tried this as well but it also fails with ( undefined method `collection_item' )</p> <pre><code>do_something_with( item.collection_item.collection_item_id ) </code></pre> <p>Edit: thanks to serioys sam for pointing out that the above is obviously wrong</p> <p>I have also tried to access other attributes in the join model, like this:</p> <pre><code>do_something_with( item.position ) </code></pre> <p>and:</p> <pre><code>do_something_with( item.collection_item.position ) </code></pre> <p>Edit: thanks to serioys sam for pointing out that the above is obviously wrong</p> <p>but they also fail.</p> <p>Can anyone advise me how to proceed with this?</p> <p>Edit: --------------------></p> <p>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.</p> <p>Currently I am working on amending my Collection model like this:</p> <pre><code>class Collection &lt; ActiveRecord::Base has_many :collection_items, :order =&gt; :position, :include =&gt; :item ... end </code></pre> <p>and changing the helper to use coll.collection_items instead of coll.items</p> <p>Edit: --------------------></p> <p>I've changed my helper to work as above and it works fine - (thankyou sam)</p> <p>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 wont sort out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload