Mike Marcus
2006-Mar-08 15:31 UTC
[Rails] ActiveRecord returning Strings with find_by_sql?
Hello, I''m new to Ruby & Rails, and I''m trying to figure out if this behavior by ActiveRecord is by design, or if I''m doing something incorrectly. Here''s the deal: I have a class based on ActiveRecord::Base that defines a method that creates a query using find_by_sql(). The fields retrieved by joins with other tables are being returned as Strings, regardless of field type. Fields from the table that the class directly corresponds to are being returned as whatever type is appropriate. Is that how it''s supposed to work? I find myself having to do a lot of conversions when I need to perform any math on the results obtained from this query. Thanks for your help, Mike Marcus Software Engineer Broadband.com mmarcus@broadband.com
Stefan Kaes
2006-Mar-08 22:54 UTC
[Rails] ActiveRecord returning Strings with find_by_sql?
Mike Marcus wrote:> Hello, > > I''m new to Ruby & Rails, and I''m trying to figure out if this behavior > by ActiveRecord is by design, or if I''m doing something incorrectly. > Here''s the deal: I have a class based on ActiveRecord::Base that > defines a method that creates a query using find_by_sql(). The fields > retrieved by joins with other tables are being returned as Strings, > regardless of field type. Fields from the table that the class > directly corresponds to are being returned as whatever type is > appropriate. Is that how it''s supposed to work? I find myself having > to do a lot of conversions when I need to perform any math on the > results obtained from this query. > > Thanks for your help, >It''s a long known deficiency of the current ActiveRecord implementation (http://dev.rubyonrails.org/ticket/240). Likely not to change very soon. -- stefan -- For rails performance tuning, see: http://railsexpress.de/blog Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
Mark Reginald James
2006-Mar-08 22:56 UTC
[Rails] Re: ActiveRecord returning Strings with find_by_sql?
Mike Marcus wrote:> I''m new to Ruby & Rails, and I''m trying to figure out if this behavior > by ActiveRecord is by design, or if I''m doing something incorrectly. > Here''s the deal: I have a class based on ActiveRecord::Base that > defines a method that creates a query using find_by_sql(). The fields > retrieved by joins with other tables are being returned as Strings, > regardless of field type. Fields from the table that the class directly > corresponds to are being returned as whatever type is appropriate. Is > that how it''s supposed to work? I find myself having to do a lot of > conversions when I need to perform any math on the results obtained > from this query.That''s how it works. You only get automatic casting for fields in the model on which find is called, and only for fields from joined tables when you use find''s :include option. You can however define custom reader and writer methods for the joined fields so that you only have to write the casting code once. -- We develop, watch us RoR, in numbers too big to ignore.
Jay Donnell
2006-Mar-09 00:11 UTC
[Rails] Re: ActiveRecord returning Strings with find_by_sql?
> That''s how it works. You only get automatic casting > for fields in > the model on which find is called, and only for > fields from joined > tables when you use find''s :include option.Isn''t it database specific? Doesn''t oracle return type information and isn''t that used to cast it? I use mysql so they are always strings for me, but I believe I read that it''s different for other db''s. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Mark Reginald James
2006-Mar-09 00:49 UTC
[Rails] Re: ActiveRecord returning Strings with find_by_sql?
Jay Donnell wrote:>>That''s how it works. You only get automatic casting >>for fields in >>the model on which find is called, and only for >>fields from joined >>tables when you use find''s :include option. > > Isn''t it database specific? Doesn''t oracle return type > information and isn''t that used to cast it? I use > mysql so they are always strings for me, but I believe > I read that it''s different for other db''s.You''re right. My reply was MySQL-centric. p273 of /Agile Web Development with Rails/ states that the Oracle adaptor will type-convert all fields fetched from the DB. -- We develop, watch us RoR, in numbers too big to ignore.
Mike Marcus
2006-Mar-09 18:47 UTC
[Rails] Re: ActiveRecord returning Strings with find_by_sql?
Thanks, Mark and Stefan for your replies! - Mike On Mar 8, 2006, at 5:56 PM, Mark Reginald James wrote:> Mike Marcus wrote: > >> I''m new to Ruby & Rails, and I''m trying to figure out if this >> behavior by ActiveRecord is by design, or if I''m doing something >> incorrectly. Here''s the deal: I have a class based on >> ActiveRecord::Base that defines a method that creates a query >> using find_by_sql(). The fields retrieved by joins with other >> tables are being returned as Strings, regardless of field type. >> Fields from the table that the class directly corresponds to are >> being returned as whatever type is appropriate. Is that how it''s >> supposed to work? I find myself having to do a lot of conversions >> when I need to perform any math on the results obtained from this >> query. > > That''s how it works. You only get automatic casting for fields in > the model on which find is called, and only for fields from joined > tables when you use find''s :include option. > > You can however define custom reader and writer methods for the > joined fields so that you only have to write the casting code once. > > -- > 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