# "fake" active record attribute

**URL:** <https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207>\
**Category:** rubyonrails-talk\
**Created:** [November 10, 2008, 5:01pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207 "2008-11-10T17:01:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 10, 2008, 5:01pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/1 "2008-11-10T17:01:55Z")

</div>

I'm gonna try to be more concise:

I do a find(:group =\> "some\_column", :select =\> "count(\*) as some\_column\_count").

I did declare in the model "attr\_accessor :some\_column\_count", but the some\_column\_count comes up as nil, even though the SQL query yields results.

I suppose the column is nil because is not a real active record attribute. I added some\_column\_count as a column in the table and the assign from the group find works, but I find the solutin ugly.

Is there a way to add an activerecord attribute in the model behaves the same as a normal one, only that it doesn't map to a real column?

Gabi Ge wrote:

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 10, 2008, 5:51pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/2 "2008-11-10T17:51:53Z")

</div>

I found attr\_internal\_accessor in ActiveSupport. [http://apidock.com/rails/Module/attr\_internal\_accessor](http://apidock.com/rails/Module/attr_internal_accessor)

I put in the invoice class attr\_internal\_accessor :containers\_count

Still no help:

> > Invoice.find(:first, :select =\> '345 as company\_id')

=\> #\<Invoice company\_id: 345\>

> > Invoice.find(:first, :select =\> '345 as containers\_count')

=\> #\<Invoice \>

I want to have containers\_count from the select to be put in the attribute containers\_count in the class.

Gabi Ge wrote:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 10, 2008, 6:25pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/3 "2008-11-10T18:25:59Z")

</div>

> I found attr\_internal\_accessor in ActiveSupport. [attr\_internal\_accessor (Module) - APIdock](http://apidock.com/rails/Module/attr_internal_accessor)
> 
> I put in the invoice class attr\_internal\_accessor :containers\_count
> 
> Still no help:
> 
> > > Invoice.find(:first, :select =\> '345 as company\_id')
> 
> =\> #\<Invoice company\_id: 345\>
> 
> > > Invoice.find(:first, :select =\> '345 as containers\_count')
> 
> =\> #\<Invoice \>

This display (ie the output of inspect) will never show attributes  
other the columns on the table. If you want to see whether a method is  
there just call it.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 10, 2008, 6:33pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/4 "2008-11-10T18:33:51Z")

</div>

Frederick Cheung wrote:

> > > > Invoice.find(:first, :select =\> '345 as containers\_count')
> > 
> > =\> #\<Invoice \>
> 
> This display (ie the output of inspect) will never show attributes other the columns on the table. If you want to see whether a method is there just call it.

It makes no difference:

> > Invoice.find(:first, :select =\> '345 as containers\_count').containers\_count

=\> nil

> > Invoice.find(:first, :select =\> '345 as id').id

=\> 345

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 10, 2008, 6:50pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/5 "2008-11-10T18:50:15Z")

</div>

> Frederick Cheung wrote:
> 
> \>\>\>\> Invoice.find(:first, :select =\> '345 as containers\_count') \>\> =\> #\<Invoice \>
> 
> \> This display (ie the output of inspect) will never show attributes \> other the columns on the table. If you want to see whether a method is \> there just call it.
> 
> It makes no difference:\>\> Invoice.find(:first, :select =\> '345 as containers\_count').containers\_count =\> nil

If you've still got an attr\_accessor for containers\_count or anything like that, this will stop the above from working.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 10, 2008, 7:05pm UTC](https://discuss.rubyonrails.org/t/fake-active-record-attribute/36207/6 "2008-11-10T19:05:29Z")

</div>

Frederick Cheung wrote:
