# Models and validation problem

**URL:** <https://www.ruby-forum.com/t/models-and-validation-problem/53621>\
**Category:** Rails\
**Created:** [February 12, 2006, 12:59am UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621 "2006-02-12T00:59:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 12, 2006, 12:59am UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/1 "2006-02-12T00:59:43Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 12, 2006, 2:04am UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/2 "2006-02-12T02:04:04Z")

</div>

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/browse\_frm/thread/b0b54ca108fb589d](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/b0b54ca108fb589d)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 13, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/3 "2006-02-13T19:23:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 13, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/4 "2006-02-13T18:44:32Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 13, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/5 "2006-02-13T19:31:20Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 14, 2006, 1:49am UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/6 "2006-02-14T01:49:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [February 13, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/models-and-validation-problem/53621/7 "2006-02-13T20:52:57Z")

</div>

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](mailto: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](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Rails

> mailing  
> list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
