Hi, it''s possible, get value of ID before save the object? I need ID concatenated with year for make new value of other column in table. I try with before_save but in this point, before_save don''t know ID. I try with after_save but in this point, the object was save, so i have to update object. two transactions to make it. I think this isn''t DRY. How make better? make DRY? Does have rails a possibility to get ID before save? PS: sorry for my bad english, i''m learning :) -- Kivanio Pereira Barbosa Cel 8121-4248 www.eiqconsultoria.com.br --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Can you get away with making your other attribute a "virtual" one--not
store it in the database at all? So for instance:
class KivianosThing < ActiveRecord::Base
def kivianos_virtual_column
id.to_s + year_attribute
end
end
?
HTH,
-Roy
On Apr 29, 2:50 pm, "Kivanio Barbosa"
<kiva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi,
>
> it''s possible, get value of ID before save the object?
>
> I need ID concatenated with year for make new value of other column in
> table.
>
> I try with before_save but in this point, before_save don''t know
ID.
>
> I try with after_save but in this point, the object was save, so i have to
> update object.
>
> two transactions to make it.
>
> I think this isn''t DRY.
>
> How make better? make DRY?
>
> Does have rails a possibility to get ID before save?
>
> PS: sorry for my bad english, i''m learning :)
>
> --
> Kivanio Pereira Barbosa
> Cel 8121-4248
>
> www.eiqconsultoria.com.br
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Kivanio Barbosa wrote:> Hi, > > it''s possible, get value of ID before save the object? > > I need ID concatenated with year for make new value of other column in > table. > > I try with before_save but in this point, before_save don''t know ID. > > I try with after_save but in this point, the object was save, so i have to > update object. > > two transactions to make it. > > I think this isn''t DRY. > > How make better? make DRY? > > Does have rails a possibility to get ID before save? > > > > PS: sorry for my bad english, i''m learning :) > >The ID doesn''t get assigned until it gets saved on create, so there will never be an id before that happens. after_save or after_create is probably your best option. Another question though is do you really need an extra column in the database? Can you dynamically create this value in your model? Such as... def special_id_field "foo_#{id}" end @thing.special_id_field #=> "foo_42" -- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 29 Apr 2008, at 22:50, Kivanio Barbosa wrote:> Hi, > > > it''s possible, get value of ID before save the object? >It''s not known until the save has happened (you can''t know whether someone else might insert something into the table which would affect the autogenerated id). Fred> I need ID concatenated with year for make new value of other column > in table. > > I try with before_save but in this point, before_save don''t know ID. > > I try with after_save but in this point, the object was save, so i > have to update object. > > two transactions to make it. > > I think this isn''t DRY. > > How make better? make DRY? > > Does have rails a possibility to get ID before save? > > > > PS: sorry for my bad english, i''m learning :) > > -- > Kivanio Pereira Barbosa > Cel 8121-4248 > > www.eiqconsultoria.com.br > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kivanio Barbosa wrote:> Hi, > > it''s possible, get value of ID before save the object?Impossible before a save. You could, however, add ''another'' id like pseudo_id and set that before hand with max_id + 1 or something -R -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m no database expert but I think that if you go this route you have to ensure that during the time you get max_id, no other inserts are happening. So going this route can not guarantee that you''ll ''always'' get the desired result. -Jason On Apr 29, 6:39 pm, Roger Pack <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Kivanio Barbosa wrote: > > Hi, > > > it''s possible, get value of ID before save the object? > > Impossible before a save. > You could, however, add ''another'' id like > pseudo_id > and set that before hand with max_id + 1 or something > -R > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hi guys,
thanks answers.
I need the column because it is a key, a uniq number to control
orders, users will use to select state of order in future.
So, the key can''t be a virtual attribute.
The max_id is a good ideia, but, i need lock table to get it and
generate the key, then insert object in databse and unlock table.
It''s Similar to use after_save to get ID, then generate key and update
objetct.
In two cases, i need two access to database to make it and in first
case, i will need lock table.
i prefer after_save in this case, i found out a loop in use it.
When i use after_save:
after_save :calc_number
def calc_number
carta = Carta.find self.id
numero = "#{self.id}/#{Time.now.year}"
carta.update_attribute(:numero, numero)
end
in this point "carta.update_attribute(:numero, numero)" was happen a
loop,
because update_attribute call Base.save and Base.save after update
call after_save again, after_save call Base.save again until rails
freeze in infinite loop.
In rails doc:
after_save()
Is called after Base.save (regardless of whether it’s a create or
update save).
I don''t know if it is a bug, but it''s impossible use update
inside the
after_save.
So, i choose use max_id and lock talbe to make it.
i like to eliminate one access to it, but i think it''s imposible for
rails because It depend of the database auto increment.
Thanks for help guys.
-Kivanio
On Apr 30, 9:49 am, Jason Amster
<jayams...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I''m no database expert but I think that if you go this route you
have
> to ensure that during the time you get max_id, no other inserts are
> happening. So going this route can not guarantee that you''ll
''always''
> get the desired result.
>
> -Jason
>
> On Apr 29, 6:39 pm, Roger Pack
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> wrote:
>
> > Kivanio Barbosa wrote:
> > > Hi,
>
> > > it''s possible, get value of ID before save the object?
>
> > Impossible before a save.
> > You could, however, add ''another'' id like
> > pseudo_id
> > and set that before hand with max_id + 1 or something
> > -R
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@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
-~----------~----~----~----~------~----~------~--~---
you may be able to setup some mysql ''auto-increment'' number or something and draw from it [at least there''s something like that in postgres, not sure about mysql]. -R On Wed, Apr 30, 2008 at 8:30 AM, Kivanio <kivanio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi guys, > > thanks answers. > > I need the column because it is a key, a uniq number to control > orders, users will use to select state of order in future. > So, the key can''t be a virtual attribute. > > The max_id is a good ideia, but, i need lock table to get it and > generate the key, then insert object in databse and unlock table. > It''s Similar to use after_save to get ID, then generate key and update > objetct. > > In two cases, i need two access to database to make it and in first > case, i will need lock table. > i prefer after_save in this case, i found out a loop in use it. > > When i use after_save: > > after_save :calc_number > > def calc_number > carta = Carta.find self.id > numero = "#{self.id}/#{Time.now.year}" > carta.update_attribute(:numero, numero) > end > > in this point "carta.update_attribute(:numero, numero)" was happen a > loop, > because update_attribute call Base.save and Base.save after update > call after_save again, after_save call Base.save again until rails > freeze in infinite loop. > > In rails doc: > > after_save() > Is called after Base.save (regardless of whether it''s a create or > update save). > > I don''t know if it is a bug, but it''s impossible use update inside the > after_save. > > So, i choose use max_id and lock talbe to make it. > > > i like to eliminate one access to it, but i think it''s imposible for > rails because It depend of the database auto increment. > > Thanks for help guys. > > > -Kivanio > > > > > On Apr 30, 9:49 am, Jason Amster <jayams...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m no database expert but I think that if you go this route you have > > to ensure that during the time you get max_id, no other inserts are > > happening. So going this route can not guarantee that you''ll ''always'' > > get the desired result. > > > > -Jason > > > > On Apr 29, 6:39 pm, Roger Pack <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > > Kivanio Barbosa wrote: > > > > Hi, > > > > > > it''s possible, get value of ID before save the object? > > > > > Impossible before a save. > > > You could, however, add ''another'' id like > > > pseudo_id > > > and set that before hand with max_id + 1 or something > > > -R > > > -- > > > Posted viahttp://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Kivanio,
there are lots of ways to update a record within rails. Here are some
ideas (and it depends on your code which will work)
#use save_without_validation.... doesn''t bypass the before_/after_
callbacks, but it does move around the validation callbacks
after_save :calc_number!
def calc_number!
#this is wasteful!
# carta = Carta.find self.id
self[:numero] = "#{self.id}/#{Time.now.year}"
self.save_without_validation
end
#you can use rail''s update_all method, and just tie it down to that
particular id
def calc_number!
Carta.update_all(" numero=
''#{self.id}/#{Time.now.year}.to_s(:db)''",
"id = #{self.id}")
end
#or you can use the rails private method that bypasses the callbacks.
# it would be nice if this was public ;) Patch perhaps?
def calc_number!
self[:numero] = "#{self.id}/#{Time.now.year}"
self.send(:create_or_update_without_callbacks)
end
Good luck Kivanio,
Adam
--------
SweetSpot.dm -- Diabetes Wellness for the Family
http://www.SweetSpot.dm
http://blog.SweetSpot.dm
On Apr 30, 8:00 am, "Roger Pack"
<rogerpack2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> you may be able to setup some mysql ''auto-increment''
number or
> something and draw from it [at least there''s something like that
in
> postgres, not sure about mysql].
> -R
>
> On Wed, Apr 30, 2008 at 8:30 AM, Kivanio
<kiva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > Hi guys,
>
> > thanks answers.
>
> > I need the column because it is a key, a uniq number to control
> > orders, users will use to select state of order in future.
> > So, the key can''t be a virtual attribute.
>
> > The max_id is a good ideia, but, i need lock table to get it and
> > generate the key, then insert object in databse and unlock table.
> > It''s Similar to use after_save to get ID, then generate key
and update
> > objetct.
>
> > In two cases, i need two access to database to make it and in first
> > case, i will need lock table.
> > i prefer after_save in this case, i found out a loop in use it.
>
> > When i use after_save:
>
> > after_save :calc_number
>
> > def calc_number
> > carta = Carta.find self.id
> > numero = "#{self.id}/#{Time.now.year}"
> > carta.update_attribute(:numero, numero)
> > end
>
> > in this point "carta.update_attribute(:numero, numero)" was
happen a
> > loop,
> > because update_attribute call Base.save and Base.save after update
> > call after_save again, after_save call Base.save again until rails
> > freeze in infinite loop.
>
> > In rails doc:
>
> > after_save()
> > Is called after Base.save (regardless of whether it''s a
create or
> > update save).
>
> > I don''t know if it is a bug, but it''s impossible
use update inside the
> > after_save.
>
> > So, i choose use max_id and lock talbe to make it.
>
> > i like to eliminate one access to it, but i think it''s
imposible for
> > rails because It depend of the database auto increment.
>
> > Thanks for help guys.
>
> > -Kivanio
>
> > On Apr 30, 9:49 am, Jason Amster
<jayams...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > I''m no database expert but I think that if you go this
route you have
> > > to ensure that during the time you get max_id, no other inserts
are
> > > happening. So going this route can not guarantee that
you''ll ''always''
> > > get the desired result.
>
> > > -Jason
>
> > > On Apr 29, 6:39 pm, Roger Pack
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> > > wrote:
>
> > > > Kivanio Barbosa wrote:
> > > > > Hi,
>
> > > > > it''s possible, get value of ID before save
the object?
>
> > > > Impossible before a save.
> > > > You could, however, add ''another'' id like
> > > > pseudo_id
> > > > and set that before hand with max_id + 1 or something
> > > > -R
> > > > --
> > > > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hi Adam,
great ideas
i solve my problem with after_create, it don''t init a loop and works
fine.
#or you can use the rails private method that bypasses the callbacks.
# it would be nice if this was public ;) Patch perhaps?
def calc_number!
self[:numero] = "#{self.id}/#{Time.now.year}"
self.send(:create_or_update_without_callbacks)
end
it''s amazing :D
I like to use it.
thank you man.
On Wed, Apr 30, 2008 at 4:27 PM, Adam Greene
<adam.greene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> Hey Kivanio,
> there are lots of ways to update a record within rails. Here are some
> ideas (and it depends on your code which will work)
>
> #use save_without_validation.... doesn''t bypass the before_/after_
> callbacks, but it does move around the validation callbacks
> after_save :calc_number!
> def calc_number!
> #this is wasteful!
> # carta = Carta.find self.id
> self[:numero] = "#{self.id}/#{Time.now.year}"
> self.save_without_validation
> end
>
> #you can use rail''s update_all method, and just tie it down to
that
> particular id
> def calc_number!
> Carta.update_all(" numero=
''#{self.id}/#{Time.now.year}.to_s(:db)''",
> "id = #{self.id}")
> end
>
> #or you can use the rails private method that bypasses the callbacks.
> # it would be nice if this was public ;) Patch perhaps?
> def calc_number!
> self[:numero] = "#{self.id}/#{Time.now.year}"
> self.send(:create_or_update_without_callbacks)
> end
>
>
> Good luck Kivanio,
> Adam
>
> --------
> SweetSpot.dm -- Diabetes Wellness for the Family
> http://www.SweetSpot.dm
> http://blog.SweetSpot.dm
>
> On Apr 30, 8:00 am, "Roger Pack"
<rogerpack2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > you may be able to setup some mysql ''auto-increment''
number or
> > something and draw from it [at least there''s something like
that in
> > postgres, not sure about mysql].
> > -R
> >
> > On Wed, Apr 30, 2008 at 8:30 AM, Kivanio
<kiva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > > Hi guys,
> >
> > > thanks answers.
> >
> > > I need the column because it is a key, a uniq number to control
> > > orders, users will use to select state of order in future.
> > > So, the key can''t be a virtual attribute.
> >
> > > The max_id is a good ideia, but, i need lock table to get it and
> > > generate the key, then insert object in databse and unlock
table.
> > > It''s Similar to use after_save to get ID, then generate
key and
> update
> > > objetct.
> >
> > > In two cases, i need two access to database to make it and in
first
> > > case, i will need lock table.
> > > i prefer after_save in this case, i found out a loop in use it.
> >
> > > When i use after_save:
> >
> > > after_save :calc_number
> >
> > > def calc_number
> > > carta = Carta.find self.id
> > > numero = "#{self.id}/#{Time.now.year}"
> > > carta.update_attribute(:numero, numero)
> > > end
> >
> > > in this point "carta.update_attribute(:numero,
numero)" was happen a
> > > loop,
> > > because update_attribute call Base.save and Base.save after
update
> > > call after_save again, after_save call Base.save again until
rails
> > > freeze in infinite loop.
> >
> > > In rails doc:
> >
> > > after_save()
> > > Is called after Base.save (regardless of whether it''s a
create or
> > > update save).
> >
> > > I don''t know if it is a bug, but it''s
impossible use update inside
> the
> > > after_save.
> >
> > > So, i choose use max_id and lock talbe to make it.
> >
> > > i like to eliminate one access to it, but i think it''s
imposible for
> > > rails because It depend of the database auto increment.
> >
> > > Thanks for help guys.
> >
> > > -Kivanio
> >
> > > On Apr 30, 9:49 am, Jason Amster
<jayams...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > > I''m no database expert but I think that if you go
this route you
> have
> > > > to ensure that during the time you get max_id, no other
inserts are
> > > > happening. So going this route can not guarantee that
you''ll
> ''always''
> > > > get the desired result.
> >
> > > > -Jason
> >
> > > > On Apr 29, 6:39 pm, Roger Pack
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> > > > wrote:
> >
> > > > > Kivanio Barbosa wrote:
> > > > > > Hi,
> >
> > > > > > it''s possible, get value of ID before
save the object?
> >
> > > > > Impossible before a save.
> > > > > You could, however, add ''another'' id
like
> > > > > pseudo_id
> > > > > and set that before hand with max_id + 1 or something
> > > > > -R
> > > > > --
> > > > > Posted viahttp://www.ruby-forum.com/.
> >
>
--
Kivanio Pereira Barbosa
Cel 8121-4248
www.eiqconsultoria.com.br
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
ah, I forgot to mention that helper! Well great, I''m glad it works! Adam -------- SweetSpot.dm -- Diabetes Wellness for the Family http://www.SweetSpot.dm http://blog.SweetSpot.dm On Apr 30, 1:42 pm, "Kivanio Barbosa" <kiva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Adam, > great ideas > > i solve my problem with after_create, it don''t init a loop and works fine. > > #or you can use the rails private method that bypasses the callbacks. > # it would be nice if this was public ;) Patch perhaps? > def calc_number! > self[:numero] = "#{self.id}/#{Time.now.year}" > self.send(:create_or_update_without_callbacks) > end > > it''s amazing :D > I like to use it. > > thank you man. > > > > On Wed, Apr 30, 2008 at 4:27 PM, Adam Greene <adam.gre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey Kivanio, > > there are lots of ways to update a record within rails. Here are some > > ideas (and it depends on your code which will work) > > > #use save_without_validation.... doesn''t bypass the before_/after_ > > callbacks, but it does move around the validation callbacks > > after_save :calc_number! > > def calc_number! > > #this is wasteful! > > # carta = Carta.find self.id > > self[:numero] = "#{self.id}/#{Time.now.year}" > > self.save_without_validation > > end > > > #you can use rail''s update_all method, and just tie it down to that > > particular id > > def calc_number! > > Carta.update_all(" numero= ''#{self.id}/#{Time.now.year}.to_s(:db)''", > > "id = #{self.id}") > > end > > > #or you can use the rails private method that bypasses the callbacks. > > # it would be nice if this was public ;) Patch perhaps? > > def calc_number! > > self[:numero] = "#{self.id}/#{Time.now.year}" > > self.send(:create_or_update_without_callbacks) > > end > > > Good luck Kivanio, > > Adam > > > -------- > > SweetSpot.dm -- Diabetes Wellness for the Family > >http://www.SweetSpot.dm > >http://blog.SweetSpot.dm > > > On Apr 30, 8:00 am, "Roger Pack" <rogerpack2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > you may be able to setup some mysql ''auto-increment'' number or > > > something and draw from it [at least there''s something like that in > > > postgres, not sure about mysql]. > > > -R > > > > On Wed, Apr 30, 2008 at 8:30 AM, Kivanio <kiva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi guys, > > > > > thanks answers. > > > > > I need the column because it is a key, a uniq number to control > > > > orders, users will use to select state of order in future. > > > > So, the key can''t be a virtual attribute. > > > > > The max_id is a good ideia, but, i need lock table to get it and > > > > generate the key, then insert object in databse and unlock table. > > > > It''s Similar to use after_save to get ID, then generate key and > > update > > > > objetct. > > > > > In two cases, i need two access to database to make it and in first > > > > case, i will need lock table. > > > > i prefer after_save in this case, i found out a loop in use it. > > > > > When i use after_save: > > > > > after_save :calc_number > > > > > def calc_number > > > > carta = Carta.find self.id > > > > numero = "#{self.id}/#{Time.now.year}" > > > > carta.update_attribute(:numero, numero) > > > > end > > > > > in this point "carta.update_attribute(:numero, numero)" was happen a > > > > loop, > > > > because update_attribute call Base.save and Base.save after update > > > > call after_save again, after_save call Base.save again until rails > > > > freeze in infinite loop. > > > > > In rails doc: > > > > > after_save() > > > > Is called after Base.save (regardless of whether it''s a create or > > > > update save). > > > > > I don''t know if it is a bug, but it''s impossible use update inside > > the > > > > after_save. > > > > > So, i choose use max_id and lock talbe to make it. > > > > > i like to eliminate one access to it, but i think it''s imposible for > > > > rails because It depend of the database auto increment. > > > > > Thanks for help guys. > > > > > -Kivanio > > > > > On Apr 30, 9:49 am, Jason Amster <jayams...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m no database expert but I think that if you go this route you > > have > > > > > to ensure that during the time you get max_id, no other inserts are > > > > > happening. So going this route can not guarantee that you''ll > > ''always'' > > > > > get the desired result. > > > > > > -Jason > > > > > > On Apr 29, 6:39 pm, Roger Pack <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > > > wrote: > > > > > > > Kivanio Barbosa wrote: > > > > > > > Hi, > > > > > > > > it''s possible, get value of ID before save the object? > > > > > > > Impossible before a save. > > > > > > You could, however, add ''another'' id like > > > > > > pseudo_id > > > > > > and set that before hand with max_id + 1 or something > > > > > > -R > > > > > > -- > > > > > > Posted viahttp://www.ruby-forum.com/. > > -- > Kivanio Pereira Barbosa > Cel 8121-4248 > > www.eiqconsultoria.com.br--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---