Forum: Ruby on Rails Nice way to set attributes to nil if blank

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Max W. (Guest)
on 2009-05-28 13:06
I've got a form for editing a model called Property.  Property has two
fields, web_color and thumbnail_path, that are nil by default, and the
model has some behaviour to generate defaults for them when nil, which
is the usual case.

When the form is submitted, the text fields for these attributes come
through as empty strings, and so these attributes are changed to be
empty strings instead of nil.

I can think of some hacky ways to stop this, such as doing something
like this in the controller:

params[:property][:web_color] = nil if
params[:property][:web_color].blank?

But, it would be better if this behaviour lived in the model.  One way
would be to set up a before_save callback like this:

before_save :set_nils

def set_nils
  self.web_color = nil if self.web_color.blank?
  self.thumbnail_path = nil if self.thumbnail_path.blank?
end

But, this feels kind of hacky and i'd like a cleaner way.  Can anyone
show me one?

thanks
max
Andrew T. (Guest)
on 2009-05-28 18:07
(Received via mailing list)
On Thu, May 28, 2009 at 11:06 AM, Max W.
<removed_email_address@domain.invalid> wrote:
> I can think of some hacky ways to stop this, such as doing something
> def set_nils
>  self.web_color = nil if self.web_color.blank?
>  self.thumbnail_path = nil if self.thumbnail_path.blank?
> end
>
> But, this feels kind of hacky and i'd like a cleaner way.  Can anyone
> show me one?
>
> thanks
> max
> --

In your model do

def web_color(web_color)
  web_color = nil if web_color.blank?
  write_attribute(:web_color, web_color)
end

and the same for thumbnail_path

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings
Max W. (Guest)
on 2009-05-28 18:32
Andrew T. wrote:

> In your model do
>
> def web_color(web_color)
>   web_color = nil if web_color.blank?
>   write_attribute(:web_color, web_color)
> end
>
> and the same for thumbnail_path
>
> Andrew T.
> http://ramblingsonrails.com
>
> http://MyMvelope.com - The SIMPLE way to manage your savings

Thanks Andrew - presumably you meant

  def web_color=(web_color)

I ended up doing this:

  before_validation :set_nils

  def set_nils
    self.web_color = nil if self.web_color.blank?
    self.thumbnail_path = nil if self.thumbnail_path.blank?
  end

Originally i had it using a before_save callback but this didn't work as
the object state with web_color = "" was failing the validation before
getting to the before_save, but with the before_validation it works.

However your way is cleaner so i'll do that instead.

thanks!
max
Andrew T. (Guest)
on 2009-05-28 18:45
(Received via mailing list)
On Thu, May 28, 2009 at 4:32 PM, Max W.
<removed_email_address@domain.invalid> wrote:
> Thanks Andrew - presumably you meant
>
>  def web_color=(web_color)
>

Sorry, I did mean web_color=(...

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings
Max W. (Guest)
on 2009-05-28 18:49
BTW, this seems like the sort of behaviour that one might want to have
quite often in a model, to prevent form submission from changing 'nil'
to "".  Maybe i'll put it into a macro, i'll post up my code here if i
do.

cheers
max
Philip H. (Guest)
on 2009-05-28 20:39
(Received via mailing list)
> BTW, this seems like the sort of behaviour that one might want to have
> quite often in a model, to prevent form submission from changing 'nil'
> to "".  Maybe i'll put it into a macro, i'll post up my code here if i
> do.

I don't recall the name, but there is a plugin on
agilewebdevelopment.com/plugins that does this automatically.

This may have been it... looks like it will do what you want in any
case.

http://agilewebdevelopment.com/plugins/stripattributes

-philip
Max W. (Guest)
on 2009-05-28 20:49
Philip H. wrote:
>> BTW, this seems like the sort of behaviour that one might want to have
>> quite often in a model, to prevent form submission from changing 'nil'
>> to "".  Maybe i'll put it into a macro, i'll post up my code here if i
>> do.
>
> I don't recall the name, but there is a plugin on
> agilewebdevelopment.com/plugins that does this automatically.
>
> This may have been it... looks like it will do what you want in any
> case.
>
> http://agilewebdevelopment.com/plugins/stripattributes
>
> -philip

Nice - thanks for that philip!
This topic is locked and can not be replied to.