insert value of primary key into another field before record is saved

Hey all,

I created a seed task:

  def books!
    books = Book.where(:type_id =>
Type.find_by_name('Fiction')).order('id')
    books.each do |book|
      @books = 4.times.map do |i|
        subbook! "#{book.book_num}-#{i}".to_s, :book_state =>
BookState[:available].id, :book => book
      end
    end
    puts "log it: #{@books.is_a?(Array)}" #array of book objects
courtesy of map
  end

  def subbook!(book_num, options = {})
    defaults = {
      :book_num => book_num
    }
    Subbook.create!(defaults.merge(options))
  end

My problem is when I create a subbook record, whatever auto
incremented primary key it gets, I want to also assign it to another
field of the same record called 'sequence'. So if the primary key of
the record is 23, then the sequence should also be 23. But the record
is not created yet, so it doesnt have an id yet.

I tried doing this in subbook model:

belongs_to :book

before_create :add_sequence

  def add_sequence
    self.sequence = self.id
  end

But that didn't do anything at all when running the seed task.

Any idea how to update another field with id of record before that
record is saved to database?

Thanks for response

John Merlino <stoicism1@...> writes:

I tried doing this in subbook model:

belongs_to :book

before_create :add_sequence

  def add_sequence
    self.sequence = self.id
  end

But that didn't do anything at all when running the seed task.

Any idea how to update another field with id of record before that
record is saved to database?

Thanks for response

How can you update a record with the id when the id doesn't exist yet? You
can't. However, you might try changing your call to after_create rather than
before_create.

Mind you, this seems like replication of the id field anyway, so what's the
point?