← Back to Upcase

How to add a new record to a related table thru the parent table in the console?

(Kim Khan) #1

parent table = cars
related table = parts

c = car.find_by_id(1)
c.part.new(:car_id => 1, :part_name => “gas pedal”, :color => “black”)

I can’t get this to work. How can I do this? How do I find what methods I can use for this relationship?

(Pedro Moreira) #2

Hi @kimkhan, I believe you can do `c.parts.build(params)

You can read more about the methods available here

(Crispin Cornett) #3
c.parts.new(params) # will also work

The key is pluralizing parts… As @pedromoreira indicated.

(Kim Khan) #4

Ok. Yes, that is a good resource. Thanks for the tip.