I've got a Rails project with these model relationships:
class Business < ActiveRecord::Base
has_many :customers, :dependent => :destroy
has_many :locations, :dependent => :destroy
end
class Customer < ActiveRecord::Base
belongs_to :business
has_one :calendar, :dependent => :destroy
end
class Calendar < ActiveRecord::Base
belongs_to :customer
has_many :events, :dependent => :destroy
end
class Event < ActiveRecord::Base
belongs_to :calendar
has_one :location
end
I have just created a model 'location.rb' which I have integrated into the business and event model relationships.
My question is split in 2:
- How can I write my location relationship so that it belongs to a business but can still be "owned"(used) by an event?
The reason I am asking this is because I need to be able to have an 'location_id' column in my events table as a foreign key.
Here is my location.rb so far:
class Location < ActiveRecord::Base
belongs_to :business
end
- If 1) is possible, how can my events migration table create the location_id foreign key?
Here is my current migration table for events:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.timestamps
t.references :calendar, foreign_key: true
#t.references :location, foreign_key: true
t.string :name
t.string :description
t.date :day
t.integer :event_id
t.datetime :starts_at
t.datetime :ends_at
end
end
end
I have commented out my attempt, but I'm not sure this would work without some kind of relationship linking the two?
Aucun commentaire:
Enregistrer un commentaire