views:

47

answers:

1

For example, in

class Student < ActiveRecord::Base
  has_many :awards
end

class Awards < ActiveRecord::Base
  belongs_to :student
end

the above should be the correct usage, but what if we use

class Student < ActiveRecord::Base
  has_many :awards
end

class Awards < ActiveRecord::Base
  has_one :student
end

doesn't the above also make possible student.awards as an array of Award objects, and award.student as a Student object which is the recipient of the award, so works the same way as the method at the top of post?

+4  A: 

has_one is used in case of a one-to-one relationship, not in one-to-many relationships.

Correct use of has_one:

class Student < ActiveRecord::Base
  has_one :id_card
end

class IdCard < ActiveRecord::Base
  belongs_to :student
end
captaintokyo
The point is: The class with the `belongs_to` statement is the class with the foreign key in its table schema.
mikezter