Hey Chris,
It’s been a while since I did anything like this, but here’s what I’d do:
rails new survey_app
cd survey_app
rails g model Survey title:string owner:references admin:references
rails g model User name:string
Next open up the create_surveys.rb
migration file and make one small edit:
Change this:
class CreateSurveys < ActiveRecord::Migration[5.0]
def change
create_table :surveys do |t|
t.string :title
t.references :owner, foreign_key: true
t.references :admin, foreign_key: true
t.timestamps
end
end
end
To this:
class CreateSurveys < ActiveRecord::Migration[5.0]
def change
create_table :surveys do |t|
t.string :title
t.references :owner
t.references :admin
t.timestamps
end
end
end
We need to knock out foreign_key: true
on both :owner
and :admin
as otherwise Rails will be looking for tables of these names
Then create and migrate the database:
rake db:create
rake db:migrate
Here you’re creating the models and specifying that there are two columns in the Survey table that will be referred to as :owner and :admin and which hold references to another table.
Next edit the model files, thus:
class Survey < ApplicationRecord
belongs_to :owner, :class_name => 'User'
belongs_to :admin, :class_name => 'User'
end
class User < ApplicationRecord
has_many :owned_surveys, :class_name => 'Survey', :foreign_key => 'owner_id'
has_many :admin_surveys, :class_name => 'Survey', :foreign_key => 'admin_id'
end
Here you are creating a property on the Survey model named :owner, then specifying that this property is related to the User
class. Rails, seeing the belongs_to :owner
, will look for a column in the surveys table called “owner_id” and use that to store the foreign key. Then you’re doing the exact same thing for the admin.
Then you are creating a property on the User Model named :owned_surveys
, specifying that this property is related to the Survey
model, and that the foreign key on the Survey
model which relates it to this property is called ‘owner_id’. Then you are doing the same thing for admin surveys.
I copied most of that explanation from here.
And that’s it. To test:
rails c
User.create(name: "Jim")
User.create(name: "Chris")
Survey.create(title: "How to baffle cats", owner_id: 1, admin_id: 2)
Survey.first().owner
=> #<User id: 1, name: "Jim", created_at: "2017-02-20 13:23:40", updated_at: "2017-02-20 13:23:40">
Survey.first().admin
=> #<User id: 2, name: "Chris", created_at: "2017-02-20 13:23:46", updated_at: "2017-02-20 13:23:46">
User.first().owned_surveys
=> #<ActiveRecord::Associations::CollectionProxy [#<Survey id: 1, title: "How to baffle cats", owner_id: 1, admin_id: 2, created_at: "2017-02-20 13:23:49", updated_at: "2017-02-20 13:23:49">]>
User.find(2).admin_surveys
=> #<ActiveRecord::Associations::CollectionProxy [#<Survey id: 1, title: "How to baffle cats", owner_id: 1, admin_id: 2, created_at: "2017-02-20 13:23:49", updated_at: "2017-02-20 13:23:49">]>
Hope that helps some.