Find method and Virtual Attributes

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!

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
[email protected]