为用户创建个人资料的最佳方法是在用户注册后立即创建个人资料
has_one :profile, dependent: :destroy
after_create :create_profil
def create_profile
@profile = Profile.create(user: self)
end
但个人资料必须包含其他信息,如经验,教育,技能...... 所以我想知道最好的方法是什么,我想使用嵌套属性这是一个很好的解决方案吗?
答案 0 :(得分:0)
您应该访问有关创建关联的rails基础知识。
检查以下链接: http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one
答案 1 :(得分:0)
我建议您将个人资料数据保留在用户模型中,因为以后访问该信息会更自然
user.skills = "some skills"
user.experience = "a lot of experience"
而不是个人资料模型
user.profile.skills = "some skills"
user.profile.experience = "a lot of experience"
但是要回答你的问题,最好的方法是创建一对一关系的配置文件模型(我假设你使用的是rails 4)
rails g model profile field1:type field2:type user:references
然后将关系添加到每个模型
has_one :profile, :dependent => :destroy
belongs_to :user
对于嵌套属性,我建议您看一下这个railscasts解释如何使用它们,然后不要自己创建,而应该使用cocoon gem使其更简单。
我希望它有所帮助:D