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 570215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:22:39+00:00 2026-05-13T13:22:39+00:00

I am trying to do the following in a Ruby on Rails project: class

  • 0

I am trying to do the following in a Ruby on Rails project:

class FoodItem < ActiveRecord::Base
  has_and_belongs_to_many :food_categories
  has_many :places, :through => :food_categories
end

class FoodCategory < ActiveRecord::Base
  has_and_belongs_to_many :food_items
  belongs_to :place
end

class Place < ActiveRecord::Base  
  has_many :food_categories
  has_many :food_items, :through => :food_category
end

But calling the instance method some_food_item.places gives me the following error:

ActiveRecord::StatementInvalid: PGError: ERROR:  column 
food_categories.food_item_id does not exist
LINE 1: ...laces".id = "food_categories".place_id    WHERE (("food_cate...

: SELECT "places".* FROM "places"  INNER JOIN "food_categories" ON "places".id = "food_categories".place_id    WHERE (("food_categories".food_item_id = 1))

Which makes perfect sense – because of the HABTMs on FoodItem and FoodCategory I have the mapping table named food_categories_food_items.

What do I have to do to get some_food_item.places to look places up correctly through the mapping table instead of looking for a food_item_id in the food_categories table?

  • 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-13T13:22:40+00:00Added an answer on May 13, 2026 at 1:22 pm

    My first version of the answer was incorrect, but this one works perfectly. I made a couple of typos the first time (the hazard of not actually creating an app to test) but this time I verified. And a plugin is needed, but this is easy. first, install the plugin:

    script/plugin install git://github.com/ianwhite/nested_has_many_through.git
    

    This installs Ian White’s workaround, and it works seamlessly. Now the models, copied directly from the test app I setup to get this working:

    class FoodItem < ActiveRecord::Base
      has_many :food_category_items
      has_many :food_categories, :through => :food_category_items
      has_many :places, :through => :food_categories
    end
    
    class FoodCategory < ActiveRecord::Base
      has_many :food_category_items
      has_many :food_items, :through => :food_category_items
      belongs_to :place
    end
    
    class FoodCategoryItem < ActiveRecord::Base
      belongs_to :food_item
      belongs_to :food_category
    end
    
    class Place < ActiveRecord::Base
      has_many :food_categories
      has_many :food_category_items, :through => :food_categories
      has_many :food_items, :through => :food_category_items
    end
    

    Now “far” associations work just as well. place_instance.food_items and food_item.places both work flawlessly, as well as the simpler associations involved. Just for reference, here’s my schema to show where all the foreign keys go:

    create_table "food_categories", :force => true do |t|
      t.string   "name"
      t.integer  "place_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    
    create_table "food_category_items", :force => true do |t|
      t.string   "name"
      t.integer  "food_item_id"
      t.integer  "food_category_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    
    create_table "food_items", :force => true do |t|
      t.string   "name"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    
    create_table "places", :force => true do |t|
      t.string   "name"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    

    Hope this helps!

    UPDATE: This question has come up a few times recently. I wrote an article, nesting your has_many :through relationships, to explain in detail. It even has an accompanying example application on GitHub to download and play around with.

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

Sidebar

Related Questions

Why do I get following error when trying to start a ruby on rails
I am trying to add the following method to the Math class in Ruby
I'm new to Ruby, and I'm trying the following: mySet = numOfCuts.times.map{ rand(seqLength) }
Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey
In Ruby, I'm trying to do the following. def self.stats(since) return Events.find(:all, :select =>
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I was trying the following example, but with external URLs: Using WebViews The example
I am trying the following code: <?php $link = mysql_connect('localhost', 'root', 'geheim'); if (!$link)
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
I've ran into a problem while trying to test following IRepository based on NHibernate:

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.