bricker
2012-Oct-11 07:16 UTC
Discussion: Inserting NULL into database instead of empty strings
When a form is submitted and handled by Rails, it doesn''t strip out empty parameters, instead just inserting those values as-is (blank strings). I''m not claiming this to be a problem or a bug, I just want to know if there is a built-in way to normalize these values to ensure clean data. I''ve been adding simple callbacks to turn those blank strings into NULL''s. Something like this: before_save :nilify_blanks def nilify_blanks self.attributes.keys.each { |a| self[a] = nil if self[a] == "" } end But it gets a little tedious to add this to every model, or mixing it in, or whatever. It doesn''t *really* matter to me if everything is blank, or if everything is NULL, I just want the consistency to avoid queries like: Event.where("title is not null and title != ''''") We have a lot of tasks running behind the scenes that are creating objects, and I don''t want to have to do something like this every time: Event.create(start_date: Time.now, title: "", description: "", location: "") I''m of the mindset that the database should be as hands-off as possible when it comes to data integrity (for example, allowing NULL in a column even if it is a required field in the application), instead leaving that up the the application via validations. It feels cleaner to me to only have one source of validation, and if we decide that a field shouldn''t be required, I don''t think that deserves a schema alteration on the database. So: does Rails have anything built-in to ensure this kind of data consistency? -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8pg5c5cKgAMJ. For more options, visit https://groups.google.com/groups/opt_out.
Johann Christoph Gorzny
2012-Oct-11 08:47 UTC
Re: Discussion: Inserting NULL into database instead of empty strings
hello, if you want a default "null" in a column you can define it in the migrations. for example: t.text :columnname, null: true hope i understand your problem. -- 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-/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 https://groups.google.com/groups/opt_out.
Scott Ribe
2012-Oct-11 15:59 UTC
Re: Discussion: Inserting NULL into database instead of empty strings
On Oct 11, 2012, at 1:16 AM, bricker wrote:> When a form is submitted and handled by Rails, it doesn''t strip out empty parameters, instead just inserting those values as-is (blank strings). I''m not claiming this to be a problem or a bug, I just want to know if there is a built-in way to normalize these values to ensure clean data.Check out the "attribute_normalizer" gem. -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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 https://groups.google.com/groups/opt_out.