Ruby on Rails Friday, April 30, 2010

Rolf Pedersen wrote:
> I'm not sure if this answers your question in regards to your specific
> example, but let's say you have a simple table with just two numerical
> columns, say two integers.
>
> SELECT i1, i2, i1+i2 AS "Sum" FROM MyTable
> will show three columns, two of them real columns from the table and one
> computed.
>
> So the term in this case is a COMPUTED column.

The term "derived column" is technically (slightly) more accurate.

ms wrote:
> Simple example: saving a user message in a corresponding table vs.
> counting all messages of a user.

1. Saving user messages in a corresponding table.

This would be called a "one-to-many relationship," or "association" for
short.

Example:
user = User.first
user.messages

2. Counting all messages of a user.

These types of calculations are called called "aggregate functions."

Example:
user = User.first
user.messages.count

2a. Caching aggregate functions.

It's also possible to store the results of aggregate functions in a
column on the parent model. Rails has built-in support for this pattern,
which is called a "counter cache."

AFAIK Rails support the counter cache only. I've occasionally wished it
had direct support for caching "sum" and "average" (avg) aggregate
functions as well, but it can still be doe manually in a similar pattern
as counter cache.
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment