I was working yesterday and realized that I couldn''t find how a model of mine was getting a default value: part = Part.new part.type # 1 I scoured my model and its observer, but such a default had not been programmed. Then I checked the schema, and there it was: :default => 1. So what''s your take? How do you feel about defining defaults in the database? If you define a default in the database should you validate the attribute anyway? Should you double-enforce the default in your model? What do you do? -- 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:>> database? If you define a default in the database should you validate >> the attribute anyway? Should you double-enforce the default in your >> model? What do you do? > > Do it in the database. That way in a year when you run some SQL > manually via some other means "just this one time" you won''t > shoot yourself in the foot.That''s a fair point. In this particular situation though the value being defaulted is a foreign key. It''s a default, but with an assumption that there''s a record in another table with an ID of 1. So it feels like that breaks any kind of safety it tried to create. I guess I can agree with defaults that aren''t foreign keys and are documented along with the class. -- 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 was working yesterday and realized that I couldn''t find how a model of > mine was getting a default value: > > part = Part.new > part.type # 1 > > I scoured my model and its observer, but such a default had not been > programmed. Then I checked the schema, and there it was: :default => 1. > > So what''s your take? How do you feel about defining defaults in the > database? If you define a default in the database should you validate > the attribute anyway? Should you double-enforce the default in your > model? What do you do?Do it in the database. That way in a year when you run some SQL manually via some other means "just this one time" you won''t shoot yourself in the foot. Wouldn''t hurt to clarify it in the model as well though or make a note of it perhaps so a year from now you remember where that''s coming from... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---