This is inside the ActiveRecord. Why doesn''t the second one work? def ssn=(value) write_attribute("ssn", value.gsub(/-/,"")) end #def ssn # val = read_attribute("ssn") # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? # return val #end
On 6/2/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is inside the ActiveRecord. Why doesn''t the second one work?What error do you get?> def ssn=(value) > write_attribute("ssn", value.gsub(/-/,"")) > end > > #def ssn > # val = read_attribute("ssn") > # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > # return val > #end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
I don''t get an error. I get 123-23-12 inserted into the db instead of 123231234 if you uncomment that. Right now, its loading the ssn but it''s missing the dashes. --Sana On 6/1/05, Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 6/2/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This is inside the ActiveRecord. Why doesn''t the second one work? > > What error do you get? > > > def ssn=(value) > > write_attribute("ssn", value.gsub(/-/,"")) > > end > > > > #def ssn > > # val = read_attribute("ssn") > > # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > > # return val > > #end > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Cheers > > Koz >
Sana, How is ssn defined in your db? varchar(9)? Ray on 6/1/2005 2:04 PM Sana Ansari said the following: I don''t get an error. I get 123-23-12 inserted into the db instead of 123231234 if you uncomment that. Right now, its loading the ssn but it''s missing the dashes. --Sana On 6/1/05, Michael Koziarski wrote: On 6/2/05, Sana Ansari wrote: This is inside the ActiveRecord. Why doesn''t the second one work? What error do you get? def ssn=(value) write_attribute("ssn", value.gsub(/-/,"")) end #def ssn # val = read_attribute("ssn") # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? # return val #end _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- Cheers Koz Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Yes, ssn is defined as varchar(9) the problem is basically this we have a form where we want the user to type in 123-23-1234 and in the database we want to store 123231234 when you go to edit the form or show the user their typing error we want them to see 123-23-1234 On 6/1/05, Ray Baxter <ray.baxter-mn4gwa5WIIQysxA8WJXlww@public.gmane.org> wrote:> > Sana, > > How is ssn defined in your db? varchar(9)? > > Ray > > on 6/1/2005 2:04 PM Sana Ansari said the following: > > I don''t get an error. I get 123-23-12 inserted into the db instead > of 123231234 if you uncomment that. Right now, its loading the ssn > but it''s missing the dashes. > > --Sana > > On 6/1/05, Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 6/2/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This is inside the ActiveRecord. Why doesn''t the second one work? > > What error do you get? > > def ssn=(value) > write_attribute("ssn", value.gsub(/-/,"")) > end > > #def ssn > # val = read_attribute("ssn") > # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > # return val > #end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > -- > Cheers > > Koz > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Maybe try write_attribute("ssn", value.gsub("-","")) On 6/1/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is inside the ActiveRecord. Why doesn''t the second one work? > > def ssn=(value) > write_attribute("ssn", value.gsub(/-/,"")) > end > > #def ssn > # val = read_attribute("ssn") > # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > # return val > #end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Joe, that is what the first function is doing and it is working correctly, however the second function seems to cause the problem, although it does work correctly on its own. The problem seems to be in active record using the ssn function to read the value of ssn before doing the insert instead of doing read_attribute("ssn")? Does this make sense? Mark On 6/1/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Maybe try > write_attribute("ssn", value.gsub("-","")) > > On 6/1/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This is inside the ActiveRecord. Why doesn''t the second one work? > > > > def ssn=(value) > > write_attribute("ssn", value.gsub(/-/,"")) > > end > > > > #def ssn > > # val = read_attribute("ssn") > > # val.gsub!(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > > # return val > > #end > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The problem is that the text_field function does not use the accessor to get its data, it uses a function called #accessorName#_before_type_case this returns the information stored with out going through your overloaded accessor. The only way I can see to by pass this is to overload the before_type_cast function ex. def ssn_before_type_cast ssn end On 6/2/05, Mark Alexander Friedgan <hubrix-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Joe, > > that is what the first function is doing and it is working correctly, > however the second function seems to cause the problem, although it does > work correctly on its own. The problem seems to be in active record using > the ssn function to read the value of ssn before doing the insert instead of > doing read_attribute("ssn")? > > Does this make sense? > > Mark > > > On 6/1/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Maybe try > > write_attribute("ssn", value.gsub("-","")) > > > > On 6/1/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This is inside the ActiveRecord. Why doesn''t the second one work? > > > > > > def ssn=(value) > > > write_attribute("ssn", value.gsub(/-/,"")) > > > end > > > > > > #def ssn > > > # val = read_attribute("ssn") > > > # val.gsub! (/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > > > # return val > > > #end > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
We are doing the same thing and the only way to make it work with the form helpers is to also alias the ssn_before_type_cast() function to do the same thing as your ssn(). Im not quite sure how this affects the rest of the AR functionality, but at first glance it works as I intended it to YMMV Chris On 6/2/05, Mark Alexander Friedgan <hubrix-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Joe, > > that is what the first function is doing and it is working correctly, > however the second function seems to cause the problem, although it does > work correctly on its own. The problem seems to be in active record using > the ssn function to read the value of ssn before doing the insert instead of > doing read_attribute("ssn")? > > Does this make sense? > > Mark > > > On 6/1/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Maybe try > > write_attribute("ssn", value.gsub("-","")) > > > > On 6/1/05, Sana Ansari <xilver72-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This is inside the ActiveRecord. Why doesn''t the second one work? > > > > > > def ssn=(value) > > > write_attribute("ssn", value.gsub(/-/,"")) > > > end > > > > > > #def ssn > > > # val = read_attribute("ssn") > > > # val.gsub! (/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3") unless val.nil? > > > # return val > > > #end > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >