Ruby on rails 带导轨的型材
为用户创建配置文件的最佳方式是什么?实际上,我是在用户注册之后创建配置文件的Ruby on rails 带导轨的型材,ruby-on-rails,ruby-on-rails-4,devise,Ruby On Rails,Ruby On Rails 4,Devise,为用户创建配置文件的最佳方式是什么?实际上,我是在用户注册之后创建配置文件的 has_one :profile, dependent: :destroy after_create :create_profil def create_profile @profile = Profile.create(user: self) end 但个人资料必须包含其他信息,如经验、教育、技能等,。。。 所以我想知道做这件事的最佳方法是什么,我想使用嵌套属性是一个很好的解决方案吗?您应
has_one :profile, dependent: :destroy
after_create :create_profil
def create_profile
@profile = Profile.create(user: self)
end
但个人资料必须包含其他信息,如经验、教育、技能等,。。。
所以我想知道做这件事的最佳方法是什么,我想使用嵌套属性是一个很好的解决方案吗?您应该访问rails创建关联的基础知识
我建议您将配置文件数据保留在用户模型中,因为以后访问这些信息会更自然
user.skills = "some skills"
user.experience = "a lot of experience"
而不是轮廓模型
user.profile.skills = "some skills"
user.profile.experience = "a lot of experience"
但要回答您的问题,最好的方法是创建一个具有一对一关系的概要文件模型(我假设您使用的是rails 4)
然后将关系添加到每个模型
models/user.rb
models/profile.rb
对于嵌套属性,我建议您查看这篇文章,其中解释了如何使用它们,然后您应该使用,而不是自己创建,以使其更简单
我希望这能有所帮助:D他已经在用户和个人资料之间建立了联系,他只是想知道是否有比他发布的更好的解决方案
rails g model profile field1:type field2:type user:references
has_one :profile, :dependent => :destroy
belongs_to :user