# Find method and Virtual Attributes

**URL:** <https://www.ruby-forum.com/t/find-method-and-virtual-attributes/179875>\
**Category:** Rails\
**Created:** [December 6, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/find-method-and-virtual-attributes/179875 "2009-12-06T12:54:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [December 6, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/find-method-and-virtual-attributes/179875/1 "2009-12-06T12:54:38Z")

</div>

Hi,

I have a model with some virtual attributes. I would like to use find as  
in invoce.lines.find(:conditions =\> ‘total = 100’) being total a virtual  
attribute. Is it possible? I would like to use find because in the  
future is it possible that total stops being a virtual attribute and I  
need to use the find.

In my opinion for find should be trasparent if the attributes in  
:conditions are virtual or real.

Any help or hint would be really appreciated.  
Thanks!

---

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [December 6, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/find-method-and-virtual-attributes/179875/2 "2009-12-06T16:28:05Z")

</div>

Juan K. wrote:

> Hi,
> 
> I have a model with some virtual attributes. I would like to use find as  
> in invoce.lines.find(:conditions =\> ‘total = 100’) being total a virtual  
> attribute. Is it possible?

Obviously not. The conditions in find are for the DB, but virtual  
attributes are by definition not stored in the DB.

You could create a virtual calculated field in the select clause…

> I would like to use find because in the  
> future is it possible that total stops being a virtual attribute and I  
> need to use the find.

Rewrite the query at that time. Don’t complicate your design based on  
what you don’t yet have.

> In my opinion for find should be trasparent if the attributes in  
> :conditions are virtual or real.

Perhaps, but that’s not the way it currently works.

An approach like that would need a lot of work to transform between real  
and virtual attributes. It would also need specific declaration of  
virtual attributes, since Rails has no concept of computed virtual  
attributes apart from methods in general.

> Any help or hint would be really appreciated.

There’s nothing to help with or hint at. You need to know the DB schema  
to use AR find.

> Thanks!

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
