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
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
–
We develop, watch us RoR, in numbers too big to ignore.
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
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!
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
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
[email protected]
On 2/14/06, Charlie B. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Rails
mailing
list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails