michael
2006-Feb-23 13:39 UTC
[Rails] find_by_sql aliasing issue when accessing attributes
I was recently having problems accessing attributes in an object. Can somebody explain to me why this is, and if it is a bug? I have the following code: @archives = RapturePost.find_by_sql "select distinct date_format(rapture_posts.date_created,''%Y-%m'') date_created from rapture_posts where rapture_posts.site_id = 1 order by rapture_posts.date_created desc" I wish to loop through the contents of the object with the following: for archive in archives #do something with the contents of archive end Whenever I try to access archive.date_created I get nothing. For some reason the "distinct date_format(rapture_posts.date_created,''%Y-%m'') as date_created" is causing the date_created to become inaccessible. When I loop through the @archives variable and do a debug I get: --- !ruby/object:RapturePost attributes: date_created: 2006-02 ... which shows the dates that I need. However, when I attempt to access archive.date_created inside the loop, it delivers me an empty string. This does not occur if I remove the quoted section above and just select date_created; but obviously, I need that date formatted clause. Any ideas? -- Posted via http://www.ruby-forum.com/.
Dave Teare
2006-Feb-23 17:47 UTC
[Rails] find_by_sql aliasing issue when accessing attributes
Not sure what the problem is exactly, but can''t you side step this issue entirely by simply selecting the "real" date and formating it in Ruby? On Thursday, February 23, 2006, at 2:39 PM, michael wrote:>I was recently having problems accessing attributes in an object. Can >somebody explain to me why this is, and if it is a bug? > >I have the following code: > >@archives = RapturePost.find_by_sql "select distinct >date_format(rapture_posts.date_created,''%Y-%m'') date_created from >rapture_posts where rapture_posts.site_id = 1 order by >rapture_posts.date_created desc" > >I wish to loop through the contents of the object with the following: > >for archive in archives >#do something with the contents of archive >end > >Whenever I try to access archive.date_created I get nothing. > >For some reason the "distinct >date_format(rapture_posts.date_created,''%Y-%m'') as date_created" is >causing the date_created to become inaccessible. > >When I loop through the @archives variable and do a debug I get: > > --- !ruby/object:RapturePost > attributes: > date_created: 2006-02 > >... which shows the dates that I need. > >However, when I attempt to access archive.date_created inside the loop, >it delivers me an empty string. This does not occur if I remove the >quoted section above and just select date_created; but obviously, I need >that date formatted clause. > >Any ideas? > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsCheers! --Dave Teare http://devlists.com - Email list management http://palmsphere.com - Apps for your hand-held -- Posted with http://DevLists.com. Sign up and save your time!
michael
2006-Feb-23 21:05 UTC
[Rails] Re: find_by_sql aliasing issue when accessing attributes
I wanted to keep it all in SQL, and thanks to some help from Greg Edwards, I found the problem. It turned out that aliasing the formatted date back to date_created was causing Rails to trip up (or Rails was just assuming that date_created world return a date rather than a formatted date as a string). Ultimately changing the alias from date_created to something else caused the problem to go away. michael Dave Teare wrote:> Not sure what the problem is exactly, but can''t you side step this issue > entirely by simply selecting the "real" date and formating it in Ruby? > > On Thursday, February 23, 2006, at 2:39 PM, michael wrote: >>I wish to loop through the contents of the object with the following: >> >>quoted section above and just select date_created; but obviously, I need >>that date formatted clause. >> >>Any ideas? >> >>-- >>Posted via http://www.ruby-forum.com/. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > Cheers! > --Dave Teare > http://devlists.com - Email list management > http://palmsphere.com - Apps for your hand-held-- Posted via http://www.ruby-forum.com/.