I am currently trying to seed some test data using the following code:
# add vehicle
trim = Trim.where(:id => 1 + Random.rand(250)).first
model_year_array = trim.model_years.all(:select => :id).collect(&:id)
vehicle = Vehicle.create(trim_id: trim.id, model_year_id: model_year_array[Random.rand(model_year_array.length - 1)])
I think it’s fairly self explanatory – essentially I want to get an array of the model_year ids that are applicable to the selected trim, select a random one, and use its id as the model_year_id of the vehicle.
The problem I am having is that the trim.model_years.all(:select => :id).collect(&:id) part is generating an error that id is ambiguous; the error is as follows:
SQLite3::SQLException: ambiguous column name: id: SELECT id FROM "model_years" INNER JOIN "model_year_trims" ON "model_years"."id" = "model_year_trims"."model_year_id" WHERE "model_year_trims"."trim_id" = 110
How can I reformat my code so that selecting the id gets the id of the model_year, and is thus not ambiguous?
When performing joins, you have to prefix colums that exists in several tables, such as id :
Should do the trick