Hi!
>Hi all,
>I need some guidance as to how to get all of the pieces working for >managing a set of children records.
>In my case, the parent model is Users. The child model is Referrer codes. >One user can create many referrer codes and hand them out. If someone >signs up to the site with one of their referrer codes, they get credit for >that new user. So, I need a controller with a set of actions where they >can maintain their referral codes.
>What is needed is a fairly simple CRUD, just limited to that user:
>I need to list just the records for the user.
>I need the user to be able to add/change/delete their own codes.
>Anyone have a link to a simple tutorial to show how to manage the children >records? I'd like it to be plain old html, no AJAX stuff yet, as I'd like >to get a full understanding of basic rails, and you know, walk before I >run.
>Many Thanks!
>Regards,
>RichAm I asking the wrong questions? It seems like many questions here don't get answered...
Same here.... I'm not sure on why... but there are a lot of unanswered
questios.
Anyway. About your question.
If you have the "has_many" relationship in place, you can access the
collection of associated objects, as simple as this:
@collection = object.child_object
In theory, you can even use paginate... but I hasn't done that.. I just
did this:
In the controller:
def mostrar
@persona = Persona.find(params[:id], :include => [:contribuyentes])
@contribuyentes_asoc = @persona.contribuyentes.find(:all,:order =>
'nombre asc')
rescue ActiveRecord::RecordNotFound
redirect_to :action => 'index'
end
And in the view:
<h2>Empresas asociadas</h2>
<table celpadding="5" cellspacing="0">
<tr>
<th align="left">
Nombre
</th>
<th align="left">
RIF
</th>
</tr>
<% par_impar = 1
for contribuyente in @contribuyentes_asoc
par_impar = 1 - par_impar %>
<tr valign="top" class="ListLine<%= par_impar %>">
<td><%= h contribuyente.nombre %></td>
<td><%= h contribuyente.rif %></td>
<td class="ListActions"><%= link_to 'Ver detalles', :controller =>
'contribuyentes', :action => 'mostrar', :id => contribuyente %></td>
</tr>
<% end %>
</table>
That's for listing
As for adding, just read here:
Anyway... the basic idea is to create the "new" object, and then
associate it to the collection of the user you want it to belong.
I can't explain longer now, got work to do,
Hope this helps,
Ildefonso Camargo