Chris
2010-Aug-16 19:18 UTC
Is it possible to set default value in a migration based upon another field''s value?
I''m familiar with setting a field''s default in the create_table within a migration via :default => some value. However, what I''m looking for is to be able to set the default value based upon the value from a different field/column. I''m not sure if this is possible or not, so any help would be greatly appreciated. The exact scenario is that I''m using devise for my authentication and I''ve extended it to have a login_id field in addition to the email field. I''d like to default it to the value of the email field when the user first creates their account and they can later choose to change its value if they decide they would rather use some value other than their email address. I know I can override the controller that devise uses to set this value, but I''m trying to see if I have any options that will not require me to create an overridden controller. Thanks in advance for any guidance and help. Chris -- 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.
Rob Biedenharn
2010-Aug-16 22:04 UTC
Re: Is it possible to set default value in a migration based upon another field''s value?
On Aug 16, 2010, at 3:18 PM, Chris wrote:> I''m familiar with setting a field''s default in the create_table within > a migration via :default => some value. However, what I''m looking for > is to be able to set the default value based upon the value from a > different field/column. I''m not sure if this is possible or not, so > any help would be greatly appreciated. The exact scenario is that I''m > using devise for my authentication and I''ve extended it to have a > login_id field in addition to the email field. I''d like to default it > to the value of the email field when the user first creates their > account and they can later choose to change its value if they decide > they would rather use some value other than their email address. I > know I can override the controller that devise uses to set this value, > but I''m trying to see if I have any options that will not require me > to create an overridden controller. > > Thanks in advance for any guidance and help. > > ChrisPerhaps you can use a before_create callback on the user (account?) model. class User before_create :default_login_id private def default_login_id self.login_id ||= self.email end end -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/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.
Greg Donald
2010-Aug-17 02:53 UTC
Re: Is it possible to set default value in a migration based upon another field''s value?
On Mon, Aug 16, 2010 at 2:18 PM, Chris <cdellinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m familiar with setting a field''s default in the create_table within > a migration via :default => some value. However, what I''m looking for > is to be able to set the default value based upon the value from a > different field/column. I''m not sure if this is possible or not, so > any help would be greatly appreciated.You can put active record code into a migration: default = Model.find(:first).name rescue nil :default => default -- Greg Donald destiney.com | gregdonald.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-/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.