Forum: Ruby on Rails find_by_sql aliasing issue when accessing attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9e08ca9606317770de53e6b7d0f60256?d=identicon&s=25 michael (Guest)
on 2006-02-23 14:39
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?
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Dave Teare (Guest)
on 2006-02-23 18:49
(Received via mailing list)
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
9e08ca9606317770de53e6b7d0f60256?d=identicon&s=25 michael (Guest)
on 2006-02-23 22:05
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
This topic is locked and can not be replied to.