# How to set virtual attributes while saving multiple models?

**URL:** <https://www.ruby-forum.com/t/how-to-set-virtual-attributes-while-saving-multiple-models/122990>\
**Category:** Rails\
**Created:** [December 13, 2007, 8:02am UTC](https://www.ruby-forum.com/t/how-to-set-virtual-attributes-while-saving-multiple-models/122990 "2007-12-13T08:02:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![theremix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@theremix](https://www.ruby-forum.com/u/theremix)\
**Post date:** [December 13, 2007, 8:02am UTC](https://www.ruby-forum.com/t/how-to-set-virtual-attributes-while-saving-multiple-models/122990/1 "2007-12-13T08:02:50Z")

</div>

i need to let the user choose a few optional parameters through  
checkboxes, that when one is checked for example, will do this on the  
Upload model:

@thumbs.update :Small =\> ‘600x800\>’

and if another checkbox is checked, may do this

@thumbs.update :Large =\> ‘1280x1024\>’

here is my form

# new.rhtml

# Upload Photo
\<% form\_for(:photo, :url =\> {:action =\> "create\_upload"}, :html =\>{ :multipart =\> true }) do |form| %\> 

Title:   
 \<%= form.text\_field :title %\>

Image:   
 \<% fields\_for "photo[upload\_attributes][]", @photo.uploads[0] do |data\_form| -%\> \<%= data\_form.file\_field :uploaded\_data %\> \<% end -%\>

\<%= submit\_tag "Upload Photo" %\>

\<% end %\>

here are my models and controllers

# photo.rb

class Photo \< ActiveRecord::Base

has\_many :uploads  
belongs\_to :user

def upload\_attributes=(upload\_attributes)  
upload\_attributes.each do |attributes|  
uploads.build(attributes)  
end  
end  
end

# upload.rb

class Upload \< ActiveRecord::Base  
belongs\_to :photo

@thumbs = { :thumb =\> ‘124x143\>’ }

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 5000.kilobytes,  
:thumbnails =\> @thumbs

validates\_as\_attachment

end

# photos\_controller.rb

def new  
@photo = Photo.new  
@photo.uploads.build  
end

def create  
@photo = Photo.new(params[:photo])  
@photo.user\_id = @user.id

```
if @photo.save
  flash[:notice] = 'Photo was successfully uploaded.'
  redirect_to :action => :gallery
else
  flash[:notice] = 'Photo failed to upload.'
  render :action => :upload
end

```

end

can you please tell me how to add these key=\>values to the @thumbs so a  
user can choose what sized images will be generated through a form like  
this:

# new.rhtml

# Upload Photo
\<% form\_for(:photo, :url =\> {:action =\> "create\_upload"}, :html =\> { :multipart =\> true }) do |form| %\> 

Title:  
 \<%= form.text\_field :title %\>

```
<p>
  Image:<br />
  <% fields_for "photo[upload_attributes][]", @photo.uploads[0] do

```

|data\_form| -%\>  
\<%= data\_form.file\_field :uploaded\_data %\>

```
<% end -%>
  <ul>
    <li><%= form.check_box :XSmall %></li>
    <li><%= form.check_box :Small %></li>
    <li><%= form.check_box :Medium %></li>
    <li><%= form.check_box :Large %></li>
  </ul>
</p>

<p>
  <%= submit_tag "Upload Photo" %>
</p>

```

\<% end %\>

or maybe this

#new.rhtml

# Upload Photo
\<% form\_for(:photo, :url =\> {:action =\> "create\_upload"}, :html =\> { :multipart =\> true }) do |form| %\> 

Title:  
 \<%= form.text\_field :title %\>

```
<p>
  Image:<br />
  <% fields_for "photo[upload_attributes][]", @photo.uploads[0] do

```

|data\_form| -%\>  
\<%= data\_form.file\_field :uploaded\_data %\>

  
- \<%= data\_form.check\_box :XSmall %\>
  
- \<%= data\_form.check\_box :Small %\>
  
- \<%= data\_form.check\_box :Medium %\>
  
- \<%= data\_form.check\_box :Large %\>
  

  
\<% end -%\>

```
</p>

<p>
  <%= submit_tag "Upload Photo" %>
</p>

```

\<% end %\>

any help would be appreciated, thanks in advance.
