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.
Charlie B. (Guest)
on 2006-02-12 01: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
Mark Reginald J. (Guest)
on 2006-02-12 03: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.
Charlie B. (Guest)
on 2006-02-13 19:44
(Received via mailing list)
Thanks!
Tirta K. Untario (Guest)
on 2006-02-13 20: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
Charlie B. (Guest)
on 2006-02-13 20: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!
Tirta K. Untario (Guest)
on 2006-02-13 21: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
removed_email_address@domain.invalid

On 2/14/06, Charlie B. <removed_email_address@domain.invalid> 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
> removed_email_address@domain.invalid
>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails
> mailing
> list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Ben M. (Guest)
on 2006-02-14 02: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.