Creating and storing records in Ruby

I’ve read through my RoR books and I can’t seem to find the solution.

This is a pretty basic, easy, and practical application for RoR. If you
guys know of any resources on the net to answer these questions let me
know.

Question 1:

Basically, I have a many-to-many relationship that I want to add records
to. I use something like this to add records in the A_B table:

c.clubs<<Club.find_all_by_name(params[:clubs])

This takes the ones that already exist and establishes a relationship
between A and B.

What if I want to take ones that don’t exist in params[:clubs] and have
those added to Club table? Is there any shortcut operator for doing that
AND also does the << operation at the same time?

Question 2

In a one-to-many relationship, where I have a_id column in my b table,
how do I take many records that have to go in b and get ActiveRecord to
either create new entries OR update the ones that exist.

Do I have to find_all and delete all of them and then reenter them into
the database. It seems like there would be smarter mechanism in Ruby for
doing this…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs