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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2005-12-07 22:26
(Received via mailing list)
Hi everyone,

Ok, first off, I've read
http://wiki.rubyonrails.com/rails/pages/HowToPopul...
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.new
person.entitlement = ??
person.save

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
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2005-12-08 14: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
thinking:

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.

Sean
This topic is locked and can not be replied to.