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 -- Posted via http://www.ruby-forum.com/.
charlie bowman wrote:> 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 > ... > 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 > endUse "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.
Thanks! On Sun, 2006-02-12 at 12:03 +1100, Mark Reginald James wrote:> charlie bowman wrote: > > 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 > > ... > > 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 > > Use "self.web_site =" instead of "web_site =". Why? Read: > http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/b0b54ca108fb589d >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/d96f7ce6/attachment.html
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 James <mrj@bigpond.net.au> wrote:> charlie bowman wrote: > > 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 > > ... > > 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 > > 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 James <mrj@bigpond.net.au> wrote: > > charlie bowman wrote: > > > 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 > > > ... > > > 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 > > > > 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. > > > > _______________________________________________ > > 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/afe6352a/attachment.html
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 theurl you> gave us... T_TI''m planning the same model validation though, thanks in> advance for the info...-- Tirta K. Untario On 2/12/06, Mark Reginald> James <mrj@bigpond.net.au> wrote: > charlie bowman wrote: > > 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 > > ... > > 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 > > 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. > > > _______________________________________________ > Rails > mailing list > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails> mailing > listRails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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 Charlie Bowman 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 James <mrj@bigpond.net.au <mailto:mrj@bigpond.net.au>> wrote: >>> charlie bowman wrote: >>> > 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 >>> > ... >>> > 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 >>> >>> 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. >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org> >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org <mailto: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