many to one relation

P. Pantouffe wrote:

- it don't understand why my code in the after_create() method did not work, the API says that ,after_create() is called after Base.save on new objects that haven't been saved yet (no record exists)", in the code:

def after_create()    colour = Colour.new    colour.name = 'colour1'    ... end

  colour is the Atribute, isn't it?

The code is called after the record is created, but the current code creates a new entry in the colour table, but does not tie it to the Car record.

Strictly speaking, their is no "colour" attribute on the Car object - there is only an association to a Colour object, so you need to pass a Colour object not a string.

class Car < ActiveRecord::Base   belongs_to :colour

  def after_create()       self.colour = Colour.find_or_create_by_name('colour 1')       self.save    end

end