has_many sample code for maintaining the child records?

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,

Rich

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, Rich

Am I asking the wrong questions? It seems like many questions here don't get answered...

Again, Thanks.

Regards, Rich

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

The questions that go unanswered here are either:

   1) Very difficult questions, of which there are very few

or

   2) Questions answered here:       http://www.pragmaticprogrammer.com/titles/rails/index.html       of which there are *many*, including yours.

I am using backgroundrb and getting the following error in the backgroundrb.log file. Can anyone point me in the right direction on what's happening here? All seems well on the machine...Postgresql is running, backgroundrb server running, etc. The code from the worker is pretty large, so I didn't want to put it out unless necessary.

"druby://localhost:42531 - #<Errno::ECONNREFUSED: Connection refused - connect(2)> - (DRb::DRbConnError)"

Thanks, Nathan

It looks like you are just getting a connection refused error. Are you sure the drb server is running on the port you want and that the clietn is using the same port? Try restarting the drb server too. That error is just because it cannot make a connection to the drb server so there is something wrong with your setup.

-Ezra

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:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.

html#M000530

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

Many thanks. I'll continue working on this.

Regards Rich

Ezra,

Thanks for taking the time to respond, I will look my setup to see if I can find something out of place.

I have some logging happening in the worker, so I know the process is being spawned. I am also running the background server in a console window, so I know that it's running. Should I be running the background server with a port specified? Is there anything else I should be doing to track this down? It seems like the process is dying after it has already been kicked off.

Nathan

You could have something within the dowork() method that is killing backgroundrb, however it doesn’t seem all too likely. I would verify with ‘netstat -anp’ that backgroundrb is listening. I’ve been using backgroundrb for 2 months now and I’ve only encountered this if indeed the server is not running.

From: rubyonrails-talk@googlegroups.com [mailto:rubyonrails- talk@googlegroups.com] On Behalf Of Tom Mornini Sent: Friday, October 13, 2006 2:42 PM To: rubyonrails-talk@googlegroups.com Subject: [Rails] Re: has_many sample code for maintaining the child records?

> Hi! > > >>> 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.

The questions that go unanswered here are either:

   1) Very difficult questions, of which there are very few

or

   2) Questions answered here:       http://www.pragmaticprogrammer.com/titles/rails/index.html       of which there are *many*, including yours.

Sigh. I have a copy of Rails Recipes. I have a copy of Ruby for Rails. I have an old v1 copy of AWDWR. Especially, my AWDWR is well worn. The examples and text in these books cover the simplest cases, for example scaffolding. They do an excellent job explaining how to build the model. They DO NOT, in my opinion, do a good enough job to cover how to glue things together correctly on a more-than-toy type of scale.

I don't mind digging around in the books. I don't mind digging around in google. I have done so. If you have a particular page number in a particular book in mind, by all means, tell me, and I'll go do more reading.

Thanks.

Regards, Rich

Hi!

> Hi!

>>> 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.The questions that go unanswered here are either:

   1) Very difficult questions, of which there are very few

or

   2) Questions answered here:      http://www.pragmaticprogrammer.com/titles/rails/index.html       of which there are *many*, including yours.

Including.... mine?, please, give me the page number, or at least the chapter, I got the v1 book (still hasn't got the v2):

http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/thread/90bfa34beb62beb4/a002279e8a74ad85?lnk=gst&q=soulhunter&rnum=1#a002279e8a74ad85

You can get to it doing a search for: auto_complete_for reimplementation in this forum.

Please read correctly: I don't want an custom "auto_complete_for" for just one autocomplete, but for MANY, and thus I don't want to write the same code, or copy and paste, and replace, and then maintain 20 or copies of the function.

If you assure that the answer to *that* question is answered in v2 book, and not in v1... I will go and buy it.

I found the book to be too basic, and too "toy-like"..... for real apps you can't depend on scaffolds and such things... yes, you use them, but they are not the "whole" app.

I ended up learning a lot more doing everything from "scratch", and not using the code generators.

Please, be more carefull when you use the "RTFM" answer to some question,

c-ya!

Ildefonso Camargo

Hi!

I just remember that this exists:

http://rorsublist.rubyforge.org/

Looks pretty usefull, I'll use part of it in a part of my project that needs to do almost the same you need.

Hope this helps,

Ildefonso Camargo

Tom Mornini wrote:

I'm really very sorry if that response sounded negative. I was answering the question "why are many questions unanswered" as factually as I knew how. Rails is a wonderful thing, but it's very difficult to grok it's subtle beauty without reading the book, as it's the de-facto manual at this point in time.

I certainly didn't mean to imply any negativity in the response, either toward any previous poster, or the list in general.

After many years on mailing lists I should have recognized that the tone of that message could be easily misinterpreted.

My most sincere apologies to any and all offended parties. I was really trying to be helpful and answer the question at hand.

Readers of list will hopefully recognize that Ezra and I both put in quite a bit of time answering questions on this list. Hopefully our history will speak louder than any misunderstanding over the intent of a single message.