Questions tagged «has-one»

4
Rails:在Rails中使用带有has_one关联的build
在此示例中,我创建了一个userno profile,然后稍后profile为该用户创建一个。我尝试将build与has_one关联一起使用,但这使它崩溃了。我看到此工作的唯一方法是使用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 Rails中有没有一种方法可以使0或1有关联?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.