Well, it does, but it hits the DB again.
On Sunday, November 4, 2012 10:43:29 AM UTC+13, Abram wrote:

When I began learning rails I found it frustrating that array and active
record relation objects could not be queried in the same fashion as
database tables. Before too much confusion let me explain.

When I want something from a database table I simply write something like:
Product.where(:color=>'black') NOW, if I put a .to_sql after that statement
I see something like "SELECT \"styles\".* FROM \"styles\" WHERE
\"styles\".\"color\" = 'black'"

So clearly, rails is magically converting that friendly statement into
something more meaningful to the backend. That's fantastic and makes my
life wonderful.

So here's my problem.. after running the query I am returned an active
record relation, which let's say is called @products. Now, let's say I want
to further break that list of products into products that are big and
small. This would require one of two things.. either I must hit the
database twice being more selective, or I must iterate over @products using
a block and such ruby magic as collect/map/select/etc...

To me neither of the above is a great option, and I must admit while
learning ruby on rails this was one of the biggest stumbling blocks for me,
and one that has produced a massive amount of stackoverflow headaches for
many others.

My suggestion to rails developers is to make rails more accessible to
beginners by allowing programmers to apply the same simplicity of writing
an active record query to querying an active record relation object or
array of model objects.

For example, I want to now take @products and say:

@products.where(:size=>"small") and have this converted automagically by
rails to the appropriate statement @products.select{|product| product.size
== 'small'}

I suppose this could work if rails were to query the object type first
before performing the query. Is the object type an array or active record
relation, and is it in the expected format (collection of appropriate
objects)? If it is, then treat it as a virtual table and instead of
performing an sql query, hit it with a select block, etc.

I know understanding the underlying ruby code is good, and at times would
be preferable to using active record style commands.. but for new users
getting off the ground, I think having both options would be beneficial.
Also, I feel it would be more consistency to the framework generally.

I have also posted this at reddit
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.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/SXFUfrCGCa8J.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 10 | next ›
Discussion Overview
grouprubyonrails-talk @
postedNov 3, '12 at 9:43p
activeNov 5, '12 at 8:38a



site design / logo © 2021 Grokbase