Hi guys, I would like comments on the following. I want to send
nested arrays of hashes so the has_many relationships update
automatically each object. This is what I''ve done.
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/
associations/association_collection.rb : Line 136
def replace(original_other_array)
other_array = original_other_array.collect {|element| if element.is_a?
Hash then create(element) else element end }
other_array.each { |val| raise_on_type_mismatch(val) }
load_target
other = other_array.size < 100 ? other_array :
other_array.to_set
current = @target.size < 100 ? @target : @target.to_set
@owner.transaction do
delete(@target.select { |v| !other.include?(v) })
concat(other_array.select { |v| !current.include?(v) })
end
end
** notice that if a Hash is received, then we create(element) **
Of course this could be dangerous, so I am looking for more advise...
In your view you would have the following. This will produce a nested
array of hashes for "score" like this (don''t worry about the
3=>
thingies...
{"commit"=>"Edit", "id"=>"2",
"evaluation"=>{
"entry_id"=>"1",
"scores"=>{"3"=>{"score"=>"6",
"evaluation_criterium_id"=>"1",
"id"=>"3", "evaluation_id"=>"2"},
"4"=>{"score"=>"5",
"evaluation_criterium_id"=>"2",
"id"=>"4", "evaluation_id"=>"2"}},
"judge_id"=>"1"}}
*** VIEW ***
<!--[form:evaluation]-->
<div>
Name of Entrant: <%= @evaluation.entry.name_of_entrant %> <br />
Category: <%= @evaluation.entry.individual_category.name %> <br />
Judge''s Name: <%= @evaluation.judge.name %>
<%= hidden_field "evaluation", "entry_id" %>
<%= hidden_field "evaluation", "judge_id" %>
<div style="margin: 1em;">
<table>
<% for @score in @evaluation.scores %>
<% fields_for "evaluation[scores][]", @score do
|scores_fields|
%>
<tr>
<td>
<%= @score.evaluation_criterium.criterium %>
</td>
<td>
<%= scores_fields.select :score, *[1..10] %>
<%= scores_fields.hidden_field :id %>
<%= scores_fields.hidden_field :evaluation_id %>
<%= scores_fields.hidden_field :evaluation_criterium_id %>
</td>
</tr>
<% end %>
<% end %>
</table>
</div>
</div>
<!--[eoform:evaluation]-->
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---