Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 144623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:16:58+00:00 2026-05-11T08:16:58+00:00

I have a data model something like this: # columns include collection_item_id, collection_id, item_id,

  • 0

I have a data model something like this:

# columns include collection_item_id, collection_id, item_id, position, etc class CollectionItem < ActiveRecord::Base   self.primary_key = 'collection_item_id'   belongs_to :collection   belongs_to :item end  class Item < ActiveRecord::Base   has_many :collection_items   has_many :collections, :through => :collection_items, :source => :collection end  class Collection < ActiveRecord::Base   has_many :collection_items, :order => :position   has_many :items, :through => :collection_items, :source => :item, :order => :position end 

An Item can appear in multiple collections and also more than once in the same collection at different positions.

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.

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 

I tried this as well but it also fails with ( undefined method `collection_item’ )

do_something_with( item.collection_item.collection_item_id ) 

Edit: thanks to serioys sam for pointing out that the above is obviously wrong

I have also tried to access other attributes in the join model, like this:

do_something_with( item.position ) 

and:

do_something_with( item.collection_item.position ) 

Edit: thanks to serioys sam for pointing out that the above is obviously wrong

but they also fail.

Can anyone advise me how to proceed with this?

Edit: ——————–>

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.

Currently I am working on amending my Collection model like this:

class Collection < ActiveRecord::Base   has_many :collection_items, :order => :position, :include => :item   ... end 

and changing the helper to use coll.collection_items instead of coll.items

Edit: ——————–>

I’ve changed my helper to work as above and it works fine – (thankyou sam)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:16:58+00:00Added an answer on May 11, 2026 at 8:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 177k
  • Answers 178k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Call Application.Exit() instead. If you really want the Close event… May 12, 2026 at 3:34 pm
  • Editorial Team
    Editorial Team added an answer String.isEmpty() was added in Java 6. In earlier versions you… May 12, 2026 at 3:34 pm
  • Editorial Team
    Editorial Team added an answer This has proved to be a hosting problem somehow the… May 12, 2026 at 3:34 pm

Related Questions

What I'm trying to do is provide a generic search capability on a table.
I think my question is best described as an example. Let's say I have
I've seen many similar questions & answers, but they've used other DB-specific tricks, or
Hey all need a lil help sorting a loop for this table out, cant

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.