Forum: Ruby on Rails Newbie question - loading DB from script - has_many relation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Sean H. (Guest)
on 2005-12-07 23:26
(Received via mailing list)
Hi everyone,

Ok, first off, I've read
and it was a huge help.  I'm loading info with no issue.

EXCEPT when I try to load info into multiple tables, i.e., adding
relationships.  I have 2 questions:

1. I have a Person model with has_many :entitlements.  Entitlement
has_and_belongs_to_many :people.  (Maybe they should both be HABTM?).
There's a entitlements_people table.  When I'm loading them via
script, what is the correct way to do so?  How do I add one?  Many?
Add on to what's already there?

person =
person.entitlement = ??

2. If the data I'm loading uses entitlement names and not IDs, how do
I specify the ID?  For instance, "wireless" is an entitlement with an
ID of 1.  In my data, I have "wireless" and not "1".  Do I create a
translation table in my script, or can I ID the entitlement based on
the name?

Thank you!

Sean H. (Guest)
on 2005-12-08 15:01
(Received via mailing list)
Nevermind.  I think I found what I need.  Had I turned 2 pages past
where I was in the Rails book, I could have avoided sending this.  I'm

person.entitlements << Entitlement.find_by_name("wireless")

Of course, enclosed in a block working on the array of entitlements.

And since there's the join table, duplicates should be handled.  I
think.  I didn't see any specific info about that in the Rails book,
but I'll check the RDocs.

This topic is locked and can not be replied to.