Tag: 有一个

Rails中has_one和belongs_to之间的区别?

我想了解RoR中的has_one关系。 比方说,我有两个模型 – Person和Cell : class Person < ActiveRecord::Base has_one :cell end class Cell < ActiveRecord::Base belongs_to :person end 我可以在Cell模型中使用has_one :person而不是belongs_to :person吗? 不一样吗?

在rails中使用has_one关联构build

在这个例子中,我创build了一个没有profile的user ,然后为这个用户创build一个profile 。 我尝试使用has_one关联构build,但是被炸毁了。 我看到这个工作的唯一方法是使用has_many 。 user应该只有最多一个profile 。 我一直在尝试这个。 我有: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end 但是当我这样做: user.build_profile 我得到的错误: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1 有轨道有0或1协会的方式吗?