Io sto cercando di fare quanto segue in un Ruby on Rails progetto:has_many: attraverso un'associazione has_and_belongs_to_many
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
Ma chiamando il metodo di istanza some_food_item.places
mi dà il seguente errore:
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))
Quale ha perfettamente senso - a causa degli HABTM su FoodItem e FoodCategory ho la tabella di mappatura denominata food_categories_food_items
.
Cosa devo fare per ottenere some_food_item.places
in modo che punti correttamente nella tabella di mapping anziché cercare un food_item_id
nella tabella food_categories
?
Aiuta sicuramente! Grazie a entrambi per la soluzione e anche per l'heads up su HABTM. Sono sicuro che questo non sarà l'unico posto in cui applicherò questa soluzione! –