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

  • SEARCH
  • Home
  • 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 3699210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:04:31+00:00 2026-05-19T05:04:31+00:00

I have setup this HABTM relationship in the past and it has worked before….Now

  • 0

I have setup this HABTM relationship in the past and it has worked before….Now it isn’t and I’m at my wits end trying to figure out what’s wrong. I’ve looked through the rails guides all day and can’t seem to figure out what I’m doing wrong, so help would really be appreciated.

I have 2 models connected through a join model and I’m trying to find records based an attribute of the associated model.

Event.rb

has_and_belongs_to_many :interests

Interest.rb

has_and_belongs_to_many :events

and a join table migration that was created like:

create_table 'events_interests', :id => false do |t|
    t.column :event_id, :integer
    t.column :interest_id, :integer
end

I tried:

@events = Event.all(:include => :interest, :conditions => [" interest.id = ?", 4 ] )

But got the error:

“Association named ‘interest’ was not found; perhaps you misspelled it?”…

which I didn’t off course.

I tried:

@events = Event.interests.find(:all, :conditions => [" interest.id = ?", 4 ] )

but got the error:

“undefined method `interests’ for #Class:0x4383348”

How can I find the Events that have an interest id of 4….I’m definitely going bald from this lol

  • 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. Editorial Team
    Editorial Team
    2026-05-19T05:04:32+00:00Added an answer on May 19, 2026 at 5:04 am

    You need to include the interests table.

    @events = Event.all(:include => :interests, :conditions => ["interests.id = ?", 4])
    

    You had it right in your post, but you didn’t pluralize interests.

    Update

    Because this answer is still getting attention, I thought it might be a good idea to update it using ActiveRecord::Relation syntax since the above way is going to be deprecated.

    @events = Event.includes(:interests).where(interests: { id: 4 })
    

    or

    @events = Event.includes(:interests).where('interests.id' => 4)
    

    And in case you want a custom condition

    @events = Event.includes(:interests).where('interests.id >= 4')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this setup in my routes: namespace :admin do resources :posts end so
I have this setup right now... RewriteRule ^/?user userpanel.php RewriteRule ^/?user/?newListing newListing.php The above
I have a setup project for my C# program, and this setup project has
I have this setup: A layer has a sprite as a child. The sprite
I have this setup: an output table where my results will end up consisting
I have this setup currently: Project A outputs a war file - has a
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I have a setup like this: http://jsfiddle.net/zzjSC/3/ and I am wondering why the surrounding
I have a setup like this: <div id=container> <div id=close>Close</div> </div> Then in my
I have a setup like this. var element = $(.module-panel, this.el); $('.module-panel:not('+element+')').hide(); What I'm

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.