# Find\_by\_sql aliasing issue when accessing attributes

**URL:** <https://www.ruby-forum.com/t/find-by-sql-aliasing-issue-when-accessing-attributes/72312>\
**Category:** Rails\
**Created:** [February 23, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/find-by-sql-aliasing-issue-when-accessing-attributes/72312 "2006-02-23T14:39:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [February 23, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/find-by-sql-aliasing-issue-when-accessing-attributes/72312/1 "2006-02-23T14:39:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [February 23, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/find-by-sql-aliasing-issue-when-accessing-attributes/72312/2 "2006-02-23T18:49:35Z")

</div>

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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Cheers!  
–Dave T.  
[http://devlists.com](http://devlists.com) - Email list management  
[http://palmsphere.com](http://palmsphere.com) - Apps for your hand-held

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [February 23, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/find-by-sql-aliasing-issue-when-accessing-attributes/72312/3 "2006-02-23T22:05:52Z")

</div>

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 T. 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/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> Cheers!  
> –Dave T.  
> [http://devlists.com](http://devlists.com) - Email list management  
> [http://palmsphere.com](http://palmsphere.com) - Apps for your hand-held
