Here''s what I''m trying to accomplish... I have a person model and that peron has a home_country. Currently, home_country is an INT in the table. What I want to be able to do is convert the INT into a Country object when I pull it from the db. In order for the HTML helper functions to work, I overloaded the object.inspect method to provide default method functionality. If I called person.home_country it should invoke the inspect and return to me whichever method I define. This works in the script/console however, it does not work when using the helper functions. I #<Country:...>. Couple of questions: Is there a better way to do this? And why do I experience different behavior in the console and the HTML results? Thanks for your help.
Hello Thomas, 2005/11/30, Thomas Shelton <twshelton@hrtoolbox.com>:> I have a person model and that peron has a home_country. Currently, > home_country is an INT in the table. What I want to be able to do is convert > the INT into a Country object when I pull it from the db. In order for the HTML > helper functions to work, I overloaded the object.inspect method to provide > default method functionality. If I called person.home_country it should invoke > the inspect and return to me whichever method I define. This works in the > script/console however, it does not work when using the helper functions. I > #<Country:...>. > > Couple of questions: Is there a better way to do this? And why do I experience > different behavior in the console and the HTML results?The console inspects return values, while ERb uses to_s. That is why you see different results from the console than on the Web. That being said, which helper are you using ? You should be doing something like this: <%= collection_select :person, :home_country_id, Country.find(:all), :id, :name %> See how I am saying the value is coming from home_country_id instead of home_country ? That's because when the values come back, the home_country_id will be updated, and when the object is saved, the DB will contain the right value. If instead you said home_country, then you'd have a problem - you'd try to assign a Fixnum to a Country instance - not good. Hope that helps ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Francois Beausoleil wrote:> Hello Thomas, > > 2005/11/30, Thomas Shelton <twshelton-hjxG0kmX7RhDPfheJLI6IQ@public.gmane.org>: > >>I have a person model and that peron has a home_country. Currently, >>home_country is an INT in the table. What I want to be able to do is convert >>the INT into a Country object when I pull it from the db. In order for the HTML >>helper functions to work, I overloaded the object.inspect method to provide >>default method functionality. If I called person.home_country it should invoke >>the inspect and return to me whichever method I define. This works in the >>script/console however, it does not work when using the helper functions. I >>#<Country:...>. >> >>Couple of questions: Is there a better way to do this? And why do I experience >>different behavior in the console and the HTML results? > > > The console inspects return values, while ERb uses to_s. That is why > you see different results from the console than on the Web. > > That being said, which helper are you using ? > > You should be doing something like this: > <%= collection_select :person, :home_country_id, Country.find(:all), > :id, :name %> > > See how I am saying the value is coming from home_country_id instead > of home_country ? That''s because when the values come back, the > home_country_id will be updated, and when the object is saved, the DB > will contain the right value. > > If instead you said home_country, then you''d have a problem - you''d > try to assign a Fixnum to a Country instance - not good. > > Hope that helps ! > -- > François Beausoleil > http://blog.teksol.info/ > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsThanks Francois. I''ll give that a shot and see how it works. I was alos looking for feedback on the approach to make sure I wasn''t heading down the wrong path. Thomas