2

Like the title said, I'm trying to get the ID of the record I just created, I tried to get it in the console but the ID is nil.

Here is the print I got of self in the console at the beginning of after_create

<Unit id: nil, uuid: "9f11be13-8d07-4471-a3bb-f87943966bbd", organization_id: 33, property_id: 430, door_number: "3", created_at: "2014-12-05 13:27:57", updated_at: "2014-12-05 13:27:57", deleted_at: nil, size: "5 1/2", door_number_int: 3, leases_count: 0, tasks_count: 0, notes: nil, state_id: 68, state_tasks_count: 2, current_lease_id: nil, next_lease_id: nil, state_tasks_serie: 1, state_tasks_serie_count: 2, price_asked: #<BigDecimal:7fc79162cb80,'0.123E3',9(18)>, availability_date: nil, comments_count: 0>

Is there a way to get access to the record ID?

This is what I tried so far

after_create
   self.save
end

before_save
  if self.id.present?
    # do stuff
  end
end

It is not very pretty but it work

To answer @MarekLipka : It doesn't cause an infinite loop because the record is created only once.

I also tried to use :

after_create
   reload

   # do stuff if self.id
end

but that doesn't seem to work.

Like @PauloFidalgo said, I can't get the id except in the after_save method. Although it is strange to me that I can get it in the second before_save (triggered by the save in after_create).

Incognito
  • 493
  • 2
  • 8
  • 22

1 Answers1

9

The id is only assigned on save, on create you are just creating the skeleton object and giving some values.

To get the id of the object just call object.id

If for some reason you want to get the id in the after_* methods, you need to use the after_save:

 after_save {id = self.id}

You could also allocate an ID in create, but you need to query the database and set the value in the variable. To achieve this you need to use a sequence in database to set the id's.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • My problem here is that I need to modify some value on the actual record. That is why i'm doing it in the before_save. But to associate a new record for another element I wanted to get the ID in the after_create and do my stuff. Plus if I save the record in the after_save, it will cause an infinite loop. If the after_create doesn't have saved the record, how come I get my id in my before save that is called by the after_create? – Incognito Dec 05 '14 at 15:50
  • @Incognito So you want to change an association when you are creating or editing a record? – Paulo Fidalgo Dec 05 '14 at 15:56
  • In fact what I wanted was to duplicate some record associated to a unit, the point is that I made a template unit and when the user create is own it copy the default association, that is why I was trying it. but doing self.tasks << Task.new(taks_to_copy.attribute) didn't seem to create the association when the main record was saved – Incognito Dec 05 '14 at 16:08
  • 1
    I will still give you the anwser because after all you answered the question, I can't get the id of the record except in after_save :) – Incognito Dec 05 '14 at 16:16