StartupInstituteChicago / Fall2013TramLai

0 stars 0 forks source link

has_and_belongs_to_many association issue #4

Closed tramxme closed 10 years ago

tramxme commented 10 years ago

I created a model Category and set it "has_and_belongs_to_many :restaurants", model Restaurant "has_and_belongs_to_many :categories, association_foreign_key: "category_id"....Create a migration restaurants_categories: class CategoriesRestaurants < ActiveRecord::Migration def change create_table :categories_restaurants, id: false, force: true do |t| t.integer :category_id t.integer :restaurant_id end end

I was able to set up the checkboxes for categories in restaurant but how do I get back and see those categories? Tried to call: restaurant.category_id, restaurant.category, restaurant.category.name without success... Thank you!!!

tramxme commented 10 years ago

I did rake db:migrate after creating a join table "categories_restaurants" but there's so much table in schema....

jkelleyj commented 10 years ago

Hi Tram - When you do a HABTM, you get an association called categories so you can do:

<% restaurant.categories.each do |category| %>
  <%= link_to category.name, category %>
<% end %>

Let me know if that helps. http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference

tramxme commented 10 years ago

It gave me this error : "Could not find table 'categories_restaurants'", even though I have created a migration for this. When I did "rails g migration categories_restaurants category_id:integer restaurant_id:integer" and then rake db:migrate, it created this: class CategoriesRestaurants < ActiveRecord::Migration def change create_table :categories_restaurants end end

I had to add in: create_table :categories_restaurants, id: false, force: true do |t| t.integer :category_id t.integer :restaurant_id

Not sure if anything I did created this problem...=(

tramxme commented 10 years ago

There is no categories_restaurants table in schema.rb after I run rake db:migrate....I've read some where that it's bad practice to edit your schema manually but I kept running migration and the table didn't show. =(

livando commented 10 years ago

1) create a test for your HABTM

describe Category do
  it { should have_and_belong_to_many(:restaurants) }
end

2) fix the migration

you're missing a 'end' in the last migration. (class, method, block all require 'end')

3) run passing test 4) re-factor

diffractometer commented 10 years ago

Migrations are sort of low hanging fruit, it's not entirely uncommon to directly edit your schema... it would be good practice to try both ways. When they get really large it makes sense to consolidate...

On Fri, Nov 15, 2013 at 2:13 PM, Don Livanec notifications@github.comwrote:

1) create a test for your HABTM

describe Category do it { should have_and_belong_to_many(:restaurants) }end

2) fix the migration

you're missing a 'end' in the last migration. (class, method, block all require 'end')

3) run passing test 4) re-factor

— Reply to this email directly or view it on GitHubhttps://github.com/StartupInstituteChicago/Fall2013TramLai/issues/4#issuecomment-28599783 .

tramxme commented 10 years ago

fixed it...Thank you!!!!!!!!!!!!!!!