On Monday, March 11, 2013 11:03:53 AM UTC+1, Colin Law wrote:
>
> On 11 March 2013 08:50, Javix <[email protected] <javascript:>> wrote:
> > I'm using a nested form to enter worked time weekly. So when creating a
> new
> > week entry of 7 days I create a form as follows:
>
> The first thing you need to work out is where the code is failing.
> For example you say that it is not displaying correctly. Is the
> problem that the data in the db is wrong or the controller is not
> fetching the data correctly or the view is not displaying it
> correctly?
>
> I displayed the generated values after modifying the code as follows:
#timesheets_controller.rb
def edit
@timesheet = current_user.timesheets.find(params[:id])
@entries = []
@timesheet.activities.each do |activity|
@entries = activity.time_entries
puts "+++++++ @entries: #{@entries.inspect}"
@timesheet.date_range.each do |week_day|
puts "++++++++++++++++++++ Looping : week_day: #{week_day}"
puts "++++++++++++++++++++ worked_days:
#{activity.worked_days.inspect}"
puts "++++++++++++++++++++ entries size: #{@entries.size}"
unless activity.worked_days.include?(week_day)
puts "!!!!!!!! BANG=> adding day: #{week_day}"
entry = activity.time_entries.build(workdate: week_day)
puts "+++++++++++ new entry to be added: #{entry.inspect}"
@entries.push(entry)
puts "++++++++++++++ Entries after adding: size:
#{@entries.size}, inspect: #{@entries.inspect}"
end
end
@entries.sort! { |a, b| a.workdate <=> b.workdate }
@entries.each do |e|
puts "++++++++++++++++++++++++++++++++++++++++++++++++"
puts "final entries size: #{@entries.size}"
puts "id: #{e.id}, workdate: #{e.workdate}, worktime:
#{e.worktime.to_s}"
puts "++++++++++++++++++++++++++++++++++++++++++++++++"
end
end
end
The problem is that after calling 'push method, I have double time_entry
created for every date that does not have work time entered:
++++++++++++++++++++ Looping : week_day: 2013-03-11
++++++++++++++++++++ worked_days: [Mon, 11 Mar 2013, Tue, 12 Mar 2013]
++++++++++++++++++++ entries size: 2
++++++++++++++++++++ Looping : week_day: 2013-03-12
++++++++++++++++++++ worked_days: [Mon, 11 Mar 2013, Tue, 12 Mar 2013]
++++++++++++++++++++ entries size: 2
++++++++++++++++++++ Looping : week_day: 2013-03-13
++++++++++++++++++++ worked_days: [Mon, 11 Mar 2013, Tue, 12 Mar 2013]
++++++++++++++++++++ entries size: 2
!!!!!!!! BANG=> adding day: 2013-03-13
+++++++++++ new entry to be added: #<TimeEntry id: nil, activity_id: 33,
workdate: "2013-03-13", worktime: nil, created_at: nil, updated_at: nil>
++++++++++++++ Entries after adding: size: 4, inspect: [#<TimeEntry id:
197, activity_id: 33, workdate: "2013-03-11", worktime:
#<BigDecimal:7fe81a6eb2c0,'0.1E1',9(36)>, created_at: "2013-03-11
14:23:04", updated_at: "2013-03-11 14:23:04">, #<TimeEntry id: 198,
activity_id: 33, workdate: "2013-03-12", worktime:
#<BigDecimal:7fe81a6ea8e8,'0.5E0',9(45)>, created_at: "2013-03-11
14:23:04", updated_at: "2013-03-11 14:23:04">, #<TimeEntry id: 199,
activity_id: 33, workdate: "2013-03-13", worktime: nil, created_at:
"2013-03-11 14:23:07", updated_at: "2013-03-11 14:23:07">, #<TimeEntry id:
199, activity_id: 33, workdate: "2013-03-13", worktime: nil, created_at:
"2013-03-11 14:23:07", updated_at: "2013-03-11 14:23:07">]
In the very and I have a collection of 12 time entries instead of 7 to
display in the partial:
#timesheets/_activity_fields.html.erb
<tr class="fields">
<td><%= f.collection_select(:task_id, @tasks, :id, :name, :prompt => true)
%></td>
<%= f.fields_for :time_entries do |builder|%>
<%= render 'time_entries_fields', :f => builder, collection: @entries %>
<% end %>
<% unless f.object.nil? || f.object.new_record? %>
<td><%= link_to_remove_fields "remove", f %></td>
<% end %>
</tr>
I have no idea why 2 objects are saved every time when calling Array push
method.
Regards
>
> Colin
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/hs6Deftcs2wJ.
For more options, visit https://groups.google.com/groups/opt_out.