Duda sobre foreign keys

Tengo una tabla con ofertas de coches:

car_offers (id, make_id, model_id…)

Y otra con marcas de coches:

makes (id, name)

Mi pregunta es que tengo que hacer exactamente para, al listar, poder
poner @car_offers.make.name porque obtengo el error este:

Mysql::Error: Unknown column ‘makes.car_offer_id’ in ‘on clause’:
SELECT car_offers.id AS t0_r0, car_offers.make_id AS t0_r1,
car_offers.model AS t0_r2, car_offers.kilometers AS t0_r3,
car_offers.power_in_kw AS t0_r4, car_offers.registration_date AS
t0_r5, car_offers.price AS t0_r6, car_offers.car_body_id AS
t0_r7, car_offers.car_type_id AS t0_r8, car_offers.fuel_id AS
t0_r9, car_offers.transmission_id AS t0_r10, car_offers.color_id
AS t0_r11, car_offers.door_id AS t0_r12, car_offers.cushion AS
t0_r13, car_offers.further_vehicle_information AS t0_r14,
car_offers.contact_name AS t0_r15, car_offers.contact_telephone
AS t0_r16, car_offers.contact_mobile AS t0_r17,
car_offers.contact_email AS t0_r18, makes.id AS t1_r0,
makes.name AS t1_r1 FROM car_offers LEFT OUTER JOIN makes ON
makes.car_offer_id = car_offers.id

Por otro lado, si en el modelo pongo esto:

class CarOffer < ActiveRecord::Base
has_one :make,
:class_name => “CarOffer”,
:foreign_key => “make_id”

Obtengo el siguiente error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.name

belongs_to :make

Si te lías con el belongs to y el has many, acuérdate de que el que
tiene el
foreign key es el que va con belongs_to y al que apunta la fk es el que
tiene has_many. Has one es como has_many, pero sólo pilla un registro

Recomendación para imprimir y colgar en la pared (cerca del monitor):
http://slash7.com/cheats/activerecord_cheatsheet.pdf

:slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs