Skip to content

Instantly share code, notes, and snippets.

@vamsipavanmahesh
Created January 30, 2016 06:23
Show Gist options
  • Save vamsipavanmahesh/b133bcfdd63fd3973c3e to your computer and use it in GitHub Desktop.
Save vamsipavanmahesh/b133bcfdd63fd3973c3e to your computer and use it in GitHub Desktop.
Hey I am trying to one-one relation in active record ..
I have a model called person
class Person < ActiveRecord::Base
has_one :personal_info
end
whose migration is like this
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.string :first_name
t.integer :age
t.string :last_name
t.timestamps null: false
end
end
end
And I have other model called personal info, whose model is this
class PersonalInfo < ActiveRecord::Base
belongs_to :person
end
migration is this
class CreatePersonalInfos < ActiveRecord::Migration
def change
create_table :personal_infos do |t|
t.float :height
t.float :weight
t.references :Person, index: true, foreign_key: true
t.timestamps null: false
end
end
end
I think I have not created the model properly, and I went ahead and changed :Person to :person in the above migration. And did
rake db:migrate.
Now in the console
vamsi = Person.first
vamsi.persona_info = PersonalInfo.create height: 57 ,weight: 75
would give me can't write to unknown attribute person_id .. can anyone help me with this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment