Rails3 one to one(1対1)

  • Terminal 起動
  • cd rails3
  • rails new OneToOne
    • 割愛
  • cd OneToOne/
  • rails generate scaffold student name:string
    • 割愛
  • rails generate scaffold profile name:string student:references
    • "references" type is a type to refer to id of other tables as a foreign key.
    • 割愛
  • $ rake db:migrate
    • 割愛
  • $ vim app/models/profile.rb
    • autoconfiguration

class Profile < ActiveRecord::Base
belongs_to :student
end

  • $ vim app/models/student.rb

class Student < ActiveRecord::Base
has_one :profile
end

  • $ vim app/views/profiles/_form.html.erb

 


  <%= f.label :student %>

 # <%= f.text_field :student %>
  <%= f.collection_select(:student_id, Student.find(:all), :id, :name) %>