问题描述
我正在构建一个为消费者和企业提供功能的应用程序.在创建模型时,我考虑的是拥有用户(消费者)和企业……每个企业也将拥有用户,但用户不一定属于企业.为了减少冗余,我会在用户"中收集姓名,电子邮件等,并在企业"中收集特定的企业信息(地址,电话).
I'm building an app that provides functionality for both consumers and businesses. In creating my models, I'm thinking of having user (consumer) and business...where each business would also have user(s), but users wouldn't necessarily belong to a business. To reduce redundancy I'd collect name, email, etc in User and specific business info (address, phone) in Business.
class Business < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :business #only if business user, not consumer
end
这是配置所需关系的正确方法吗?
Is this the correct way to configure the desired relationship?
然后,当需要进行商业注册时,是否有可能(以及如何)在我的业务对象首先是用户然后是用户的地方具有嵌套的表单,所以我要按该顺序收集信息?我发现的所有示例/信息都显示了设置,其中首先捕获了用户信息,然后捕获了所有子模型.
Then when it comes time for business registration, is it possible (and how) to have nested forms where my business object is first, then user...so I'm collecting information in that order? All examples/info I've found shows the setup with user info captured first, then any sub-models.
在我的以下示例中,这是否正确:
In my following example, would this be correct:
<%= form_for(@business) do |f| %>
#grab business info
<%= f.fields_for :user do |ff| %>
#grab user info
感谢您的时间和帮助.
推荐答案
您将需要
class Business < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :business #only if business user, not consumer
attr_accessible :business_attributes
accepts_nested_attributes_for :business
end
且表单应为
<%= form_for(@user) do |f| %>
#grab user info
<%= f.fields_for :business_attributes do |ff| %>
#grab business info
然后在更新方法中使用update_attributes更新用户
then use update_attributes in update method to update user
这篇关于模型和嵌套形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!