Forum: Ruby on Rails models and validation problem

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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-02-12 00:59
In my model, I am attempting to correct some bad data rather than just
pass an error message back to the user.  The problem is that my fix
seems to get the data past validation but it then puts the bad data into
the database rather than the new(and improved) data.  If I pass in
web_site with the value of 'www.test.com', it will pass validation but
it won't add http:// to the variable.  Here is my model

class Comment < ActiveRecord::Base
  belongs_to :post


  ## validation checks
  validates_presence_of :name, :comment



  protected
  def validate
    web_site = 'http://' + web_site if web_site =~ /^www\./i
    errors.add(:web_site," - Please begin all web addresses with
http://") unless web_site.blank? || web_site =~ /^http:\/\//i
  end
end
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-12 02:04
(Received via mailing list)
charlie bowman wrote:
> http://") unless web_site.blank? || web_site =~ /^http:\/\//i
>   end
> end

Use "self.web_site =" instead of "web_site =".  Why?  Read:
http://groups.google.com/group/comp.lang.ruby/brow...

--
We develop, watch us RoR, in numbers too big to ignore.
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-13 18:44
(Received via mailing list)
Thanks!
45e815356bbcbb7bc165235679cd160d?d=identicon&s=25 Tirta K. Untario (Guest)
on 2006-02-13 19:23
(Received via mailing list)
I'm new to Ruby, and I still don't understand the discussion in the
url you gave us... T_T

I'm planning the same model validation though, thanks in advance for the
info...

--
Tirta K. Untario
4e1cda7ca71ce8ae801331a30c319257?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-02-13 19:31
(Received via mailing list)
Here's the jist of it.  When coding in your models, always preface a
variable with self., if that variable is an attribute of your model.  If
you try to change the value of a attribute of your model without
referencing it with self, you will be creating a new local variable.
Hope this helps.  Thanks Mark!
783130010bd0f961daf701904d992d89?d=identicon&s=25 Tirta K. Untario (Guest)
on 2006-02-13 20:52
(Received via mailing list)
Oh ok, now I got it ^^
I'm still confused with variable scopes in Ruby, I'll try to dig more
on this subject.

--
Tirta K. Untario
tirta@untario.com

On 2/14/06, Charlie Bowman <cmbowma@castlebranch.com> wrote:
> Here's the jist of it.  When coding in your models, always preface a
> variable with self., if that variable is an attribute of your model.  If you
> try to change the value of a attribute of your model without referencing it
> with self, you will be creating a new local variable.  Hope this helps.
> Thanks Mark!
>
>
> On Tue, 2006-02-14 at 01:22 +0700, Tirta K. Untario wrote:
> I'm new to Ruby, and I still don't understand the discussion in the
url you
> gave us... T_T

I'm planning the same model validation though, thanks in
> advance for the info...

--
Tirta K. Untario


On 2/12/06, Mark Reginald
> > web_site with the
> unless web_site.blank? || web_site =~ /^http:\/\//i
> We develop, watch us RoR, in numbers too big to ignore.
>
>
> _______________________________________________
> Rails
> mailing list
> Rails@lists.rubyonrails.org
>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails
> mailing
> list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-14 01:49
(Received via mailing list)
Huh? Now I'm even more confused. I thought that discussion was about the
potential confusion caused by Ruby's algorithm
for deciding whether an identifier is a method or a variable. I thought
"self" was for attaching a method to the class.
Why would I prepend a variable with "self"... and wouldn't I want my
model's instance variables to be "@" variables?

b
This topic is locked and can not be replied to.