我的資料表關聯是這樣的
新增兩個model(原本的events在前面幾篇就建好了)
$ rails g model category cname:string status:boolean $ rails g model event_category event_id:integer category_id:integer $ bin/rake db:migrate
編輯app/models/event.rb
class Event < ActiveRecord::Base
has_many :event_categories, ->{ order("category_id") }, :dependent => :delete_all
has_many :categories, :through => :event_categories
end
class Category < ActiveRecord::Base
has_many :event_categories, :dependent => :delete_all
has_many :categories, :through => :event_categories
end
編輯app/models/event_category.rb
class EventCategory < ActiveRecord::Base
belongs_to :event
belongs_to :category
end
如此一來,三個資料表就關聯起來了。
沒有留言:
張貼留言