I don't follow you.
I find all the options based on the idea of a trim (a style). Think
about how a honda civic ex might have different options then a honda
civic lx. In the real app you would only select a subset of those
options to assign to your vehicle. Vehicle is kind of like an
instance of a Trim, but it's different and has it's own model. So I
am grabbing all the options by trim (options =
Option.find_all_by_trim_id(t.id)) and then assigning them to a vehicle
(v.options = options or v.options_ids = options_ids).
You are right I could of done t.options instead of
Option.find_all_by_trim_id, but I have tried both. I replaced:
Option.find_all_by_trim_id with options = t.options and I have 47 in
the array of options, but 94 are saved, 2 each exactly.
It works fine if I do:
options.each {|o|
OptionsVehicle.create({'option_id' => o.option_id, 'vehicle_id' =>
v.id})
}
and explicitly create the join table values.
I have confused rails...I guess.
Erik
On Jun 20, 11:40 pm, Eric <[email protected]> wrote:
> On Jun 20, 5:18 pm, erik <[email protected]> wrote:
>
>
>
> > I have tried this both ways and each time same results, duplicate
> > entries.
>
> > Here is the simple test:
> > t = Trim.find(:first, :conditions => ['id > 65000000'])
> > options = Option.find_all_by_trim_id(t.id)
> > puts options.size
> > v = NewVehicle.new({:trim_id => t.id})
> > v.option_ids = options.map {|o| o.id}
> > v.save
>
> > I also tried it just with v.options = options, using option_ids was
> > just another attempt to make this work
>
> > The size of options in this case is 47. options_vehicles which is my
> > join table will have 94 results exactly 2 of each one option. So if
> > the option_id = 6 and vehicle_id is 4 i have 6,4 in the join table
> > twice.
>
> > I am doing one thing a little odd, but i have no idea what's causing
> > this issue. My models look like this:
> > Option.rb
> > set_primary_key "option_id"
> > has_many :options_vehicles
> > has_many :vehicles, :through => :options_vehicles
>
> > I have to use option_id as the primary key and it's a string. This is
> > other data i'm importing and I can't change the way it's organzied.
>
> > Vehicle.rb
> > has_many :options_vehicles,
> > has_many :options, :through => :options_vehicles
>
> > My join table looks like a habtm table because it was. I have tried
> > this both ways activerecord continues to put in dupes.
>
> > I actually went into the code at
> > /data/www/production/vendor/rails/activerecord/lib/active_record/
> > associations.rb:1392
> > and put prints in there and see it showing the size of the records to
> > save as 47, but it's going through here twice.
>
> > I have option_id in options marked as the primary key with a unique
> > index in mysql.
>
> > Any help would be appreciated I feel like I'm going crazy...
> > Erik
>
> You don't need to do the Options.find because you already have
> t.options. This may also explain the dupes.
>
> -eric
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---