Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
从我对一对多关系的阅读中,我们需要一个has_many在父方和一个belongs_to在子方。我想知道如果我只对关系的一部分感兴趣并且例如belongs_to在我的模型中声明一侧,rails 是否会产生错误或什么?
has_many
belongs_to
不会有错误,
has_many并且belongs_to只是在调用它们的类上自动生成关联方法。
例如:
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base end # works User.first.posts # error, method undefined Post.first.user