# Whats the best way to do this?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324>\
**Category:** Rails\
**Created:** [April 20, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324 "2006-04-20T17:16:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/1 "2006-04-20T17:16:26Z")

</div>

I have an archive that can have 0 or more extra files associated with  
it.

I would like to be able to edit the associated files info on the same  
page as the archive info.

Listing the extra files info to the edit form is simple enough, but How  
do I form the text input fields for easy (or automatic) pick-up in the  
update?

Or am I barking up the wrong tree here?

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/2 "2006-04-20T18:03:55Z")

</div>

First, you may want to check

[http://www.catb.org/~esr/faqs/smart-questions.html#bespecific](http://www.catb.org/~esr/faqs/smart-questions.html#bespecific)

I think you are asking how to display/update a collection of items. When  
you  
talk about files are you talking about a table that contains information  
about files? Do you have a file model (File.rb)? Do you have something  
associated with these files, perhaps an archive model (Archive.rb)?

In any case you will likely have a loop in your view to display the 0 or  
more files and a loop in your controller to capture the updates.

Please include additional information to get more specific help. Also  
you  
may want to post code fragments.

-Kelly

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/3 "2006-04-20T19:21:01Z")

</div>

> Tables are ‘archives’ and ‘extra\_files’
> 
> ## Related models:
> 
> class Archive \< ActiveRecord::Base  
> belongs\_to :category  
> has\_many :extra\_file  
> end

Do model operations work properly for you in script/console?  
This should probable be:

```
   has_many :extra_files

```

(note the pluralization)

> class ExtraFile \< ActiveRecord::Base  
> belongs\_to :archive  
> end

> ## Archive Controller currently looks like this:
> 
> class ArchiveController \< ApplicationController  
> scaffold :archive
> 
> ```
> def list
> @header = "Test List"
> @subheader = "Everything in the database. Using Adam's
> 
> ```

layout"

> ```
> #@programs = Archive.find_all
> @program_pages, @programs = paginate :archive
> end
> 
> def edit
> @program = Archive.find(@params["id"])
> @categories = Category.find_all
> end
> 
> ```
> 
> end

I haven’t used the scaffold function. I’ve always generated the  
scaffolds.

I guess you are using the update action that comes with the scaffold  
function. This is not likely to work, but I can’t be sure since I  
haven’t  
done it. It is not likely to work because the scaffold logic does not  
handle  
relationships to other models. You will need to put your own update  
action  
in place. In fact, I think you will need to replace all of the scaffold  
generated actions with your own versions. You can get a jump start on  
this  
by using “script/generate scaffold…”

I don’t think there is much point discussing the rest until you get an  
update action.

-Kelly

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/4 "2006-04-20T19:57:14Z")

</div>

Kelly F. wrote:

> > ## Related models:
> > 
> > class Archive \< ActiveRecord::Base  
> > belongs\_to :category  
> > has\_many :extra\_file  
> > end
> 
> Do model operations work properly for you in script/console?  
> This should probable be:
> 
> ```
> has_many :extra_files
> 
> ```
> 
> (note the pluralization)

Seems to be working fine. Lists properly in both the list and edit  
pages.  
Will try changing though…

Changing it caused errors in my listings. Would have to change them too.

\> I don't think there is much point discussing the rest until you get an \> update action. \> 
## Generated update function:

def update  
@archive = Archive.find(params[:id])  
if @archive.update\_attributes(params[:archive])  
flash[:notice] = ‘Archive was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @archive  
else  
render :action =\> ‘edit’  
end  
end

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/5 "2006-04-20T18:57:43Z")

</div>

Ok, Sorry about the non specific subject title.

Tables are ‘archives’ and ‘extra\_files’

## Related models:

class Archive \< ActiveRecord::Base  
belongs\_to :category  
has\_many :extra\_file  
end

class ExtraFile \< ActiveRecord::Base  
belongs\_to :archive  
end

## Archive Controller currently looks like this:

class ArchiveController \< ApplicationController  
scaffold :archive

```
def list
	@header = "Test List"
	@subheader = "Everything in the database. Using Adam's layout"
	#@programs = Archive.find_all
	@program_pages, @programs = paginate :archive
end

def edit
	@program = Archive.find(@params["id"])
	@categories = Category.find_all
end

```

end

Now here is the part(s) where I’m at a loss. Below is the edit.rhtml.  
I’ve re-edited the text-input fields so many times, I don’t know what I  
haven’t done yet…

Originaly the text-input fields were named by hand, and mostly didn’t  
render at all without errors.

You probably don’t need the entire edit.rhtml, but I included it all so  
you know about where I started from. The relivent part is near the  
bottom, near ‘Extra Files’

Thanks

edit.rhtml

 Edit Program Listing 
# Edit Program Listing

**Title**

**Author**

**Format**

**Reference**

**Release Date**

**Description  
 \<%= @program.description %\>**

 

**Misc (extended Description)**  
 \<%= @program.misc %\>

**Baud**  
 \<% ['2000','300','2000 BlueRam'].each do |baud| -%\> \>\<%= baud %\> \<% end -%\>

**Archival Status**

**File Location**

 

**Extra Ram**  
 Yes /\> No /\>

**Category:**  
 \<% @categories.each do |category| -%\> \>\<%= category.name %\> \<% end -%\>

**Extra Files**

\<% @program.extra\_file.each do |file| -%\>  
Name: \<%= text\_field “extra\_file”, “name”, “index” =\> file.id,  
“value”=\> file.name %\>  
Location: \<%= text\_field “extra\_file”, “location”, “index” =\> file.id,  
“value”=\> file.location %\>

\<% end -%\>

[Show](https://www.ruby-forum.com/program_downloads/Archive/archive/show/<%=%20@program.id%20%>) | \<%= link\_to "Back", :action =\> "list" %\>

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/6 "2006-04-20T20:50:42Z")

</div>

Seems to be working fine. Lists properly in both the list and edit  
pages.  
Will try changing though…

Changing it caused errors in my listings. Would have to change them too.

Hmmm. Rails has conventions on names so I surprised this works.  
Normally:

- ModelName.rb will access a database table called model\_names.
- Association _collections_ are pluralized, such as has\_many :members
- Non collection associations are singular, such as belongs\_to :group

Of course, changing a model association will break other code. I  
surprised  
the model works. I’ve been caught a few times with associations defined  
backwards, and rails doesn’t help you by throwing an exception - it  
simply  
fails to work. In other words, I used a belongs\_to when I should have  
used  
has\_many, or vice versa.

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/7 "2006-04-20T20:41:29Z")

</div>

Now, your view needs to iterate over the associated items. Here is an  
example:

Members:  
\<% for login in ordered\_members(@group) %\> \<%= select\_tag('people[]', options\_for\_select(user\_list\_for\_select(@group), login)) %\>   
\<% end %\> \<% new\_user\_select\_tag = select\_tag('people[]', options\_for\_select(user\_list\_for\_select(@group))) 5.times do %\> \<%= new\_user\_select\_tag %\>   
\<% end %\>

This is a ui for adding/removing people from a group.

Note the ‘people[]’. These people ids will be delivered to the  
controller as  
an array.

Here’s the update action:

def update  
@group = Group.find(params[:id])  
Group.transaction(@group) do  
@group.group\_members = group\_members\_from\_params(@group)  
if @group.errors.empty? and  
@group.update\_attributes(params[:group])  
flash[:notice] = ‘Group was successfully updated.’  
redirect\_to :action =\> ‘list’, :id=\>@group.period  
else  
render :action =\> ‘edit’  
end  
end  
end

Oh, and here’s a related function

def group\_members\_from\_params(group)  
group\_members = []  
unless params[:people].blank?  
params[:people].each do |login|  
unless login.blank?  
gm = (GroupMember.find\_by\_login\_and\_period\_id(login,  
group.period\_id) ||  
GroupMember.new(:group =\> group, :login =\> login))  
if gm.group\_id != group.id  
group.errors.add\_to\_base(login + ’ is assigned to another  
group’)  
else  
group\_members \<\< gm  
end  
end  
end  
end  
group\_members  
end

This may be too complicated to use as an example, but I don’t have time  
to  
make it more succinct.

-Kelly

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/8 "2006-04-20T21:25:18Z")

</div>

Kelly F. wrote:

> Now, your view needs to iterate over the associated items. Here is an  
> example:

Unfortunatly, you were right. Too complex for me to pickout the relivent  
information, re: my set-up.

Sorry.

As for the extra\_file. I wasn’t sure how to name it. It worked that way,  
so I left it.

I’m still not sure the input fields on the edit page are named  
properly…

Lance

---

<div class="post-metadata">

**Author:** ![komb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@komb](https://www.ruby-forum.com/u/komb)\
**Post date:** [April 20, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-do-this/58324/9 "2006-04-20T20:53:45Z")

</div>

I surprised = I’m surprised

(sometime fingers type what they want)
