I'm having a little bit of trouble truly understanding how this concept would work. First I'm going to use the devise gem for user authentication and activation, but I need to know how my migrations should look. I have a unique key (user will be given) that will be used to activate an account.
So here's what my code looks like VERY early on, mind you:
User Model
class User < ActiveRecord::Base
has_one :safe
has_many :contacts
end
Safe Model
class Safe < ActiveRecord::Base
belongs_to :user
end
CreateSafes Migration file
class CreateSafes < ActiveRecord::Migration
def change
create_table :saves do |t|
t.string :safe_key
t.timestamps
end
end
end
CreateUsers Migration
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
end
I'm really just not sure if I add a user_id value to the safe migration table, or whether I just use the safe_key, since that's going to be a totally unique value. Any help and wisdom would be appreciated. Thanks.
Yes, manually add user_id to the safe table. In associations, rails stores foreign keys in the table with the :belongs_to
Then you could use safe.user
to get the user, or user.safe
to get the safe.