Reputation: 61
I have "vehicles" table which looks like this
ID | MAKE | MODEL
1 | acura | cl
2 | acura | el
3 | acura | integra
4 | audi | a3
... | ... | ...
and I need to make two tables from it, "makes" table, taking only "make" columns information from "vehicles" table and putting it into "make_name" column, which I successfully created by myself(also made it to not repeat itself by uniq method).
id | make_name
1 | acura
2 | audi
3 | bmw
... | ...
And second table("make_models") which I already made, but have problems getting needed information into it. Table looks like this
id | make_id | model_name
id | makes id to which current model belongs | model from vehicles table
I tried to insert this lines to rails console
@vehicles = Vehicle.all
@makes = Make.all
@vehicles.each do |v|
MakeModel.create :make_id => @current_make = @makes.where(:make_name => v.make).id, :model_name => v.model
end
But got this error message
NoMethodError: Make Load (1.0ms) SELECT "makes".* FROM "makes" WHERE "makes"."make_name" = ? [["make_name", "ACURA"]] undefined method `id' for #
Upvotes: 0
Views: 499
Reputation: 1555
Try this
MakeModel.create :make_id => @current_make = @makes.where(:make_name => v.make).first.id, :model_name => v.model
Upvotes: 1
Reputation: 24337
The problem is that you are tying to call id
on a collection of records here:
@makes.where(:make_name => v.make).id
If you want to find the first record by make_name, use find_by
instead:
@makes.find_by(:make_name => v.make).id
Upvotes: 2