validates_each

Hi all,

I am trying to do a little checking before a user profile is created and
see if some fields are blank. If they, that's OK, I just need to set
them to a dummy value if they are, not blank. The reason for this is
that later on on another profile page they can change the value if the
user wants through a edit_in_place helper, but that doesn't do much good
if the fields are blank. I have this validates_each, and while the
method gets executed correctly, I need it to set some of the variables
for the User object which this method is a part of, but I am unsure on
how to do that. This is what I have tried but it doesn't work.

validates_each :company :on => :save do|record, attr, value|
       if value.blank?
         value = "Click to Edit"
         self.attr = value
       end
     end

Any sujestions? Thanks,

-S

The example Ryan Bates uses looks like:

validates_presence_of :state, :if => :in_us?

def in_us?
  country == 'US'
end

have a look at this if you get a minute -> http://railscasts.com/episodes/41

I'm sure you can piece together a conditional validation from what he
explains in the webcast.

ebrad wrote:

The example Ryan Bates uses looks like:

validates_presence_of :state, :if => :in_us?

def in_us?
  country == 'US'
end

The problem with that is that I have many, many fields that I need to
check which is why the way that I have I like so much, because it looped
through each field in just a few lines of code. The way that I had it
before was a huge method with a bunch of if's checking to see which
fields were blank - it was sloppy and ugly, which is why I tried to find
a better way to do it. The problem with the way that I have now is that
I can't set each field to another via this validation. Thanks,

-S

@Shandy: You only want to use the validates_xxx methods for validation
(error checking), not for assignment. The simplest route to go might
be to alter your db fields and provide default values for the
attributes in question. You should do that through a migration so
that you don't lose visibility to the assignments in your code base.

Otherwise, consider something like this:

class MyModel
  DEFAULTS = {:attr1=>default_1, :attr2=>default_2, ... }
  before_create :set_defaults
  ...
  private
  def set_defaults
    DEFAULTS.each do |attr, default_value|
      self.send("#{attr}=", default_value) unless
self.send(attr).blank?
    end
  end
end

Defaults is a hash of attributes (the keys) and their default values.
You iterate over that and the loop assigns the default value if it
finds the attribute blank.

AndyV wrote:

class MyModel
  DEFAULTS = {:attr1=>default_1, :attr2=>default_2, ... }
  before_create :set_defaults
  ...
  private
  def set_defaults
    DEFAULTS.each do |attr, default_value|
      self.send("#{attr}=", default_value) unless
self.send(attr).blank?
    end
  end
end

I tried to get this to work and I'm not sure what I am doing wrong but
it doesn't. Everything prints out ok but nothing gets set. I am saying
before_save, that's is the only difference in code. Any suggestions of
what I am doing wrong? Thx,

-S

AndyV wrote:

class MyModel
  DEFAULTS = {:attr1=>default_1, :attr2=>default_2, ... }
  before_create :set_defaults
  ...
  private
  def set_defaults
    DEFAULTS.each do |attr, default_value|
      self.send("#{attr}=", default_value) unless
self.send(attr).blank?
    end
  end
end

I answered my own question. I changed the unless self.send(attr).blank?
part. I did this:

  def set_defaults
     DEFAULTS.each do |attr, default_value|
     if self.send(attr).blank?
        self.send("#{attr}=", default_value)
        puts attr
        puts default_value
     end
    end
  end

But thanks for the help Andy, this is exactly what I was looking for.

-S

Sorry about that. In my mind I was thinking about assigning the
values ahead of time so they'd be present in the form. As you figured
out, I should have used an 'if' instead of an 'unless'. Good catch!