在一个模型上只使用belongs_to与在一个模型上使用has_many和在另一个模型上使用belongs_to有什么区别?
例如:
class Author < ActiveRecord::Base
end
class Book < ActiveRecord::Base
belongs_to :author
end对比
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :author
end谢谢。
发布于 2016-02-13 01:53:38
猜测每个方法将有助于向关联类添加一组不同的附加方法。
对于belongs_to,如果不得不猜测,您可以在一定程度上获得对Book实例调用关联的能力:
@book.author使用has_many时,如果我不得不猜测,您可以在一定程度上调用Author实例上的关联:
@author.books还有,to
和
many
万一有兴趣的话
https://stackoverflow.com/questions/35375106
复制相似问题