0

I have a User model and a Tracker model. User has_many Trackers and Tracker belongs_to User. The User model has a virtual attribute called tracker_date that I would like to be able to use in a before_save callback in the Tracker model. The callback is supposed to set the date in the Tracker model for all of the Tracker records updated at the same time through the nested attributes association. Is this possible? Or is there a better way to do what I am trying to do?

The view:

<%= form_for @user, :url => {:controller => 'trackers'}, :method => 'post' do |f| %>

<%= f.label :tracker_date, "Week ending" %>
<%= f.text_field :tracker_date %> <br />

    <% @user.top_items.each do |item| %>
    <%= f.fields_for :trackers, item.trackers.build do |g| %>

    <%= item.top_item_name %>   

    <%= g.text_field :value %>
    <%= g.hidden_field :position_id, :value => item.id %>  <br />
    <% end %>
    <% end %>

<%= f.submit "Submit" %>

<% end %>

User.rb

has_many :trackers
accepts_nested_attributes_for :trackers
attr_accessor :tracker_date
attr_accessible :tracker_date, :trackers_attributes

Tracker.rb

belongs_to :user
attr_accessible :date
before_save :set_date

def set_date
    self.date = self.user.tracker_date
end

In this example, the date does not get set in the Tracker model with the value in the virtual attribute from the User model. I also tried a simple validation in the Tracker model using the virtual attribute from the User model but that did not work either. Is there a way to get it to work or is there a better way to do this that either avoids using the virtual attribute or allows the nested model to access the virtual attribute?

Thanks.

2
  • In your form_for, why is the controller 'trackers' and not the default 'users'? I'm thinking that when the user clicks Submit, the tracker record is being saved but not the user recored. So the tracker_date isn't being set perhaps? What happens if you use the default and do the tracker fields using fields_for :tracker within the user form_for?
    – lurker
    May 24, 2013 at 13:14
  • Good thought, and thanks, but that isn't the issue. In the trackers controller, it is just doing @user.update_attributes(params[:user]). I've tested that a record will save to both the user model and the tracker model successfully from the form. It is only the virtual attribute that I am having trouble with.
    – Josh
    May 24, 2013 at 13:37

1 Answer 1

0

I had this problem. In your sample code, self.user.tracker_date is referencing a different instance of the user, and so doesn't have the expected value of the virtual attribute.

My solution is not very clean:

Create an after_save callback in the user. That callback will loop through all the trackers and set its date. You can use update_column when setting the date so that you skip the tracker validations.

By setting the tracker date in the User model, you should be able to see the value of the virtual attribute as you've set it before saving anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.