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
On Jun 20, 5:18 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> 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... > ErikYou don''t need to do the Options.find because you already have t.options. This may also explain the dupes. -eric
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 <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 20, 5:18 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> 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
I''m in Rails 2.2.2 if that matters. Erik On Jun 21, 8:27 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> 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 <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Jun 20, 5:18 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> 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
What are the associations for the Trim model, wrt Options and Vehicles? I''m also assuming that the NewVehicle model is a typo and it''s supposed to be just Vehicle.new in the snippet. I''m thinking that you need either uniqueness validations or to dedupe manually in your save/updates, but it''s hard to tell so far. -eric On Jun 21, 8:29 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> I''m in Rails 2.2.2 if that matters. > Erik > > On Jun 21, 8:27 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote: > > > 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 <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Jun 20, 5:18 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> 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 > >
NewVehicle is STI off of Vehicle. It doesn''t matter if you use Vehicle or NewVehicle the outcome is the same. Trim definition: class Trim < ActiveRecord::Base ## Associations belongs_to :model has_many :colors has_many :options so it has_many options, which is why t.options would of worked along with find the options by trim id. class Vehicle < ActiveRecord::Base def Vehicle: ## Associations belongs_to :trim belongs_to :base_exterior_color, :class_name =>''Colors'', :foreign_key => ''exterior_color_base_id'' has_many :options_vehicles, :foreign_key => :vehicle_id has_many :options, :through => :options_vehicles I think you are on the right track. Since a vehicle belongs to a trim, that trim already has every single option available for that car attached to it. What I want to do is say only X amount of those options are actually on this Vehicle though. Vehicle is more of an instance of a trim. I do need to store the trims though...i can''t just make them a pure virtual class. Unique doesn''t work exactly. It''s fine on the read, but i still have double values in the join table. It must be something to do with the fact trim has_many options and vehicles belong to a trim? i would assume this would work as i''m saving into a join table. Erik On Jun 21, 9:29 am, Eric <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What are the associations for the Trim model, wrt Options and > Vehicles? I''m also assuming that the NewVehicle model is a typo and > it''s supposed to be just Vehicle.new in the snippet. > > I''m thinking that you need either uniqueness validations or to dedupe > manually in your save/updates, but it''s hard to tell so far. > > -eric > > On Jun 21, 8:29 am,erik<e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote: > > > I''m in Rails 2.2.2 if that matters. > >Erik > > > On Jun 21, 8:27 am,erik<e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote: > > > > 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 <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Jun 20, 5:18 pm,erik<e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> 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