John Merlino
2011-Jun-27 03:21 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andrew Skegg
2011-Jun-27 05:24 UTC
Re: insert value of primary key into another field before record is saved
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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
dv
2011-Jun-27 17:32 UTC
Re: insert value of primary key into another field before record is saved
thanks for response after_create didn''t work either. And even when I call save to trigger the callback: @subbook = Subbook.create!(defaults.merge(options)) @subbook.save It still didn''t work. On Jun 27, 1:24 am, Andrew Skegg <andrewsk...-BUHhN+a2lJ4@public.gmane.org> wrote:> 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?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.