1

From my readings for a one to many relation we need a has_many on the parent side and a belongs_to on the child side. I was wondering does rails create errors or something if I am just interested in one part of the relation and just for example declare the belongs_to side in my model ?

4

1 に答える 1

3

There will be no errors,

has_many and belongs_to just auto generate association methods on the class they are called on.

For example:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
end

# works
User.first.posts

# error, method undefined 
Post.first.user
于 2013-02-05T21:10:07.343 に答える