1

This is a follow-up to this question where's the appropriate place to handle writing the current_user.id to an object I have the following model. An item which has_many assets. I'm am using accepts_nested_attributes_for :assets in the item.

I'd like to assign the current_user.id value to each asset. Normally I just do @item.update_attributes(params[:item]) to save it. Is there a simple one line way of setting the user_id for each asset in this scenario?

Looking in dev log, I see the value here:

 item[assets_attributes][10][asset]

Should I just iterate through all of these and set a user_id value?

thx

here's some more of the html (items -> menu_item; had left out above to simplify). The proposed controller sol'n below does not seem to work. I'm fine with doing at the level of controller. Any help appreciated.

 <div class='image-row'>
    <input id="menu_item_assets_attributes_18_asset" name="menu_item[assets_attributes][18][asset]" type="file" />
    <input id="menu_item_assets_attributes_18_description" name="menu_item[assets_attributes][18][description]" size="30" type="text" />
    </div>
  <div class='image-row'>
    <input id="menu_item_assets_attributes_19_asset" name="menu_item[assets_attributes][19][asset]" type="file" />
    <input id="menu_item_assets_attributes_19_description" name="menu_item[assets_attributes][19][description]" size="30" type="text" />
   </div>
 <div class='image-row'>
     <a href="/images/371/5ea19b9cfa534f1c1f5457ebb149d4259a662f88_huntbch_original.jpg?1329917713"><img alt="D5cc413a1748fb43b0baa2e32e29b10ac2efda10_huntbch_thumb" src="/images/371/d5cc413a1748fb43b0baa2e32e29b10ac2efda10_huntbch_thumb.jpg?1329917713" /></a>
<div class='img-row-description'>
     <label for="menu_item_assets_attributes_20_description">Description</label>
     <input id="menu_item_assets_attributes_20_description" name="menu_item[assets_attributes][20][description]" size="60" type="text" value="here is my comment" />
     <label for="menu_item_assets_attributes_20_destroy">Destroy</label>
     <input name="menu_item[assets_attributes][20][_destroy]" type="hidden" value="0" /><input id="menu_item_assets_attributes_20__destroy" name="menu_item[assets_attributes][20][_destroy]" type="checkbox" value="1" />
  </div>
Community
  • 1
  • 1
timpone
  • 19,235
  • 36
  • 121
  • 211

2 Answers2

1

This answer is probably a little bit muddier than your previous one.

If each asset must have an item, then it might be more sensible to remove the idea of an owning user from an asset entirely: you can always find the owning user by querying the attached item, something like @asset.item.user. However, if users can own assets independently of items, I don't think this will work for you.

If assets are always created in a nested manner for items, a before_create for the asset could assign the value you want. Something like this in asset.rb:

before_create :assign_user

def assign_user
  self.user = self.item.user if self.item && self.item.user
end

Finally, if you just want to do it in the controller, Wolfgang's answer is really good and will add the user_id to each asset_attributes.

Veraticus
  • 15,944
  • 3
  • 41
  • 45
  • Our schema requires multiple associations (ie can own an item and also own an asset separately and in different contexts to facilitaet things like group ownership). Since, that's true we want to have asset ownership exist separately from the item. – timpone Feb 23 '12 at 19:02
0

How about iterating over the params array and setting the value like so:

params[:item][:assets_attributes].map! do |asset_params| 
  asset_params[:user_id] = current_user.id 
end

Now you can use update_attributes( params[:item] ) as before.

Wolfgang
  • 4,865
  • 2
  • 29
  • 29
  • does that syntax handle the id value in this case? I'm getting an error `can't convert Symbol into Integer`; thx for help! trying to pick up rails quickly – timpone Feb 23 '12 at 18:32