1

here are the additions that I have made to my models:

class Event < ApplicationRecord
  has_one :lineup
  has_many :artists, :through => :lineup
  belongs_to :venue
end

class Lineup < ApplicationRecord
  belongs_to :artist
  belongs_to :event
end

class Artist < ApplicationRecord
  has_many :events, :through => :lineups
end

class Venue < ApplicationRecord
  has_many :events
end

not asking for help with generating migrations for all of these associations, but could you at least show me how to do it for Event?

sivanes
  • 713
  • 1
  • 13
  • 22

3 Answers3

1

Please find below migrations for Event & Lineup (that have the keys to enable your model associations):

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.references :venue, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

class CreateLineups < ActiveRecord::Migration
  def change
    create_table :lineups do |t|
      t.references :artist, index: true, foreign_key: true
      t.references :event, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

To generate them, you can use:

rails g migration create_events venue:references
rails g migration create_lineups artist:references event:references

If Event & Lineup already exist, you can generate the migrations as follows:

rails g migration add_reference_to_events venue:references
rails g migration add_references_to_lineups artist:references event:references

Migrations generated should be as follows:

class AddReferenceToEvents < ActiveRecord::Migration
  def change
    add_reference :events, :venue, index: true, foreign_key: true
  end
end

class AddReferencesToLineups < ActiveRecord::Migration
  def change
    add_reference :lineups, :artist, index: true, foreign_key: true
    add_reference :lineups, :event, index: true, foreign_key: true
  end
end
sebsonic2o
  • 356
  • 2
  • 7
  • I didn't specify it in my post, but all of these models have already exist. Could you please rewrite your response as addition of association to existing models? Thank you! – sivanes Mar 11 '16 at 19:24
1

belongs_to will place the foreign key in the declaring model whereas has_one will place it in the other model. There are good resources in this out there that I would recommend taking a look at. Here's one.

So for the event model I would do the following:

$ rails g migration AddVenueToEvents

Then fill it in with:

class AddVenueToEvents < ActiveRecord::Migration
  def change
    add_reference :events, :venue, index: true, foreign_key: true
  end
end

I would strongly recommend making use of the something like the Shoulda gem in combination with RSpec as it provides extremely valuable feedback about what you should be doing. Which would allow you to write some specs:

RSpec.describe Events, type: :model do

  #Associations
  it { should belong_to(:venue) }
  it { should have_one(:lineup) }
  it { should have_many(:artists).through(:lineup) }

The awesome thing is that once you run the your specs shoulda/rspec will give you extremely useful feedback in the terminal essentially telling you where a required Foreign Key may be missing. The message might look something like this:

Region should have a city
     Failure/Error: should belong_to(:city)
       Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.)
     # ./spec/models/region_spec.rb:5:in `block (2 levels) in <top (required)>'

as shown in this other SO post which is somewhat related.

Community
  • 1
  • 1
Ben Hawker
  • 949
  • 8
  • 15
0

Please check this migration for events.

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.belongs_to :venue, index: true
      t.timestamps null: false
    end
  end
end
  • I didn't specify it in my post, but all of these models have already exist. Could you please rewrite your response as addition of association to existing models? Thank you! – sivanes Mar 11 '16 at 19:24