I have a model with a attribute named "code". Every time i create an instance I want the "code" to be initialized by making a call to "generate_code" method. How do i initialize model attributes in rails ?? thanks for help. -- 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 -~----------~----~----~----~------~----~------~--~---
Jigar Gosar wrote:> I have a model with a attribute named "code". Every time i create an > instance I want the "code" to be initialized by making a call to > "generate_code" method. How do i initialize model attributes in rails ?? > > thanks for help.Add this line to your model class: before_create :generate_code generate_code will be called when you ''save'' an new instance just before it is sent to the database. There are a number of these methods, called CallBacks available. You may what to look through a list of them and see if there isn''t one that might work better for your particular situation. -- 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 want a call back on object construction, not on save or on create. but for now i think before_create will work for me. John Miller wrote:> Jigar Gosar wrote: >> I have a model with a attribute named "code". Every time i create an >> instance I want the "code" to be initialized by making a call to >> "generate_code" method. How do i initialize model attributes in rails ?? >> >> thanks for help. > > Add this line to your model class: > before_create :generate_code > > generate_code will be called when you ''save'' an new instance just before > it is sent to the database. There are a number of these methods, called > CallBacks available. You may what to look through a list of them and > see if there isn''t one that might work better for your particular > situation.-- 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 -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld
2007-Aug-26 07:34 UTC
Re: How do I assign default values to model attribute
Jigar Gosar wrote:> I want a call back on object construction, not on save or on create. but > for now i think before_create will work for me. > > John Miller wrote: >> Jigar Gosar wrote: >>> I have a model with a attribute named "code". Every time i create an >>> instance I want the "code" to be initialized by making a call to >>> "generate_code" method. How do i initialize model attributes in rails ?? >>> >>> thanks for help. >> >> Add this line to your model class: >> before_create :generate_code >> >> generate_code will be called when you ''save'' an new instance just before >> it is sent to the database. There are a number of these methods, called >> CallBacks available. You may what to look through a list of them and >> see if there isn''t one that might work better for your particular >> situation.how about using class Klass def initialize(default_att1, att2, att3) @default_att = default_att1 @att2 = att2 end end whenever you pass a .new method, initialize is called: var = Klass.new(10, '''', ''value'') var is now var.default_att1 = 10 var.att2 = '''' var.att3 = ''value'' helps? -- 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 -~----------~----~----~----~------~----~------~--~---
Sounds good, I try it. I hope it dosen''t conflict with the default constructor of AR class. Shai Rosenfeld wrote:> class Klass > > def initialize(default_att1, att2, att3) > @default_att = default_att1 > @att2 = att2 > end > > end > > whenever you pass a .new method, initialize is called: > > var = Klass.new(10, '''', ''value'') > > var is now > > var.default_att1 = 10 > var.att2 = '''' > var.att3 = ''value'' > > helps?-- 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 -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld
2007-Aug-26 09:23 UTC
Re: How do I assign default values to model attribute
use the super special keyword; it inherits the parent''s class methods, so it takes the super method, and modifies it for your needs: http://jamescrisp.blogspot.com/2006/09/rails-and-initialize-method.html (in the link-example, you don''t need the "if" clause supplied there) Jigar Gosar wrote:> Sounds good, I try it. I hope it dosen''t conflict with the default > constructor of AR class. > > Shai Rosenfeld wrote: >> class Klass >> >> def initialize(default_att1, att2, att3) >> @default_att = default_att1 >> @att2 = att2 >> end >> >> end >> >> whenever you pass a .new method, initialize is called: >> >> var = Klass.new(10, '''', ''value'') >> >> var is now >> >> var.default_att1 = 10 >> var.att2 = '''' >> var.att3 = ''value'' >> >> helps?-- 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 -~----------~----~----~----~------~----~------~--~---