Sean Hussey
2005-Dec-07 21:22 UTC
Newbie question - loading DB from script - has_many relationships
Hi everyone, Ok, first off, I''ve read http://wiki.rubyonrails.com/rails/pages/HowToPopulateYourDbFromScript 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
Sean Hussey
2005-Dec-08 12:58 UTC
Re: Newbie question - loading DB from script - has_many relationships
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
On 12/7/05, Sean Hussey
<seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi everyone,
>
> Ok, first off, I''ve read
> http://wiki.rubyonrails.com/rails/pages/HowToPopulateYourDbFromScript
> 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
>