Rails 3中数据建模帮助 - 有很多,属于,嵌套Atrributes [英] Rails 3 Data Modeling Help - Has Many, Belongs to, Nested Atrributes
问题描述
我的工作涉及三个型号(收件人,奖,播音员)的一个项目,需要有一个嵌套的属性由播音员给多个收件人发出一个奖项时。举个例子,奖励形式必须做的三件事情的能力:
I am working on a project involving three models (recipient, award, announcer) and need to have a nested attributes when issuing an award by an announcer to multiple recipients. For an example, award form need to have the ability to do 3 things:
- 可以添加多个收受人(即添加收件人,删除收件人) - 嵌套的属性
- 创建一个新的奖项之后,该奖项将被张贴到收件人的个人资料。
- 启用的未来投票@ recipient.awards和@ announcer.awards
真正的如何巧妙地解决这个问题方面的斗争。中大奖的形式:收件人accepts_nested_attributes_for下面的数据结构,一种是有道理的,但不能做。你能帮我吗?提前很多感谢。
Really struggle in terms of how to smartly solve this problem. The following data structure kind of made sense, however can not do "accepts_nested_attributes_for :recipients" in the award form. Can you help? Many thanks in advance.
类收件人<的ActiveRecord :: Base的
class Recipient < ActiveRecord::Base
- 的has_many:奖项
- 的has_many:播音员,:通过=>:奖项
结束
类播音员&LT;的ActiveRecord :: Base的
class Announcer < ActiveRecord::Base
- 的has_many:奖项
- 的has_many:收件人:通过=>:奖项
结束
类奖&LT;的ActiveRecord :: Base的
class Award < ActiveRecord::Base
- belongs_to的:播音员
- belongs_to的:收件人
结束
推荐答案
你只是在那里。主要的问题是,你正在尝试的形式,而不是仅仅创造奖和其他对象(用户)之间的关系,创建收件人对象。你可以做这样的事情:
You're just about there. The main issue is that you're trying to create recipient objects in the form rather than just creating a relationship between the award and another object (user). You could do something like this:
class User < ActiveRecord::Base
has_many :recipients
has_many :awards, :through => :recipients
end
# this is your relationship between an award and a user
class Recipient < ActiveRecord::Base
belongs_to :user
belongs_to :award
end
class Award < ActiveRecord::Base
has_many :recipients
has_many :users, :through => :recipients
belongs_to :announcer
accepts_nested_attributes_for :recipients, :allow_destroy => true
end
class Announcer < ActiveRecord::Base
has_many :awards
has_many :recipients, :through => :awards
end
然后你只需做一个嵌套形式,将建立recipients_attributes数组:
Then you would just do a nested form that would build the recipients_attributes array:
<%= form_for @award do |f| %>
<%= f.text_field :name %>
<div id="recipients">
<% @award.recipients.each do |recipient| %>
<%= render :partial => '/recipients/new', :locals => {:recipient => recipient, :f => f} %>
<% end %>
</div>
<%= link_to_function 'add recipient', "jQuery('#recipients').append(#{render(:partial => '/recipients/new').to_json})" %>
<% end %>
和,使其保持干燥只是把部分嵌套到部分:
And, to keep it DRY just push the nested part into a partial:
# app/views/recipients/_new.html.erb
<% recipient ||= Recipient.new %>
<%= f.fields_for 'recipients_attributes[]', recipient do |rf| %>
<%= rf.select :user_id, User.all %>
<%= fr.check_box '_delete' %>
<%= fr.label '_delete', 'remove' %>
<% end %>
显然User.all通话是不理想的,所以也许作出这样的自动完成。
Obviously the User.all call isn't ideal so maybe make that an autocomplete.
这篇关于Rails 3中数据建模帮助 - 有很多,属于,嵌套Atrributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!