C
C
Ceyoto2015-03-13 16:05:01
Ruby on Rails
Ceyoto, 2015-03-13 16:05:01

How to write data to two tables?

There are two tables (cars and colors).
The cars table has
id brand power color_id fields
The colors table has
id color rarity fields
The content of the models

class Car < ActiveRecord::Base
  belongs_to :color
  ...
end

class Color < ActiveRecord::Base
  ...
end

After creating the basic data about the car, we can move on to creating additional characteristics. How to write in the color_id field the id of the created car color when creating color characteristics?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Misyagin, 2015-03-18
@Ceyoto

model car
belongs_to :color
controller
@car = Car.new(:power => 106)
@car.color = Color.where(:id => 6).first
@car.save Advertisement
site?

S
Satisfied IT, 2018-01-10
@rafamont

This is just a report on the conversion of a project from a previous version to a newer one, close it and move on. You can read what kind of warnings, maybe something useful will be there.

D
Dmitry Eremin, 2018-01-10
@EreminD

put the picture, please, on habrastorage - nothing is visible,
but so far no errors are visible,
one warning,
but you can live and work with it further

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question