开发者

What is an ActiveRecord Rails Relation really?

开发者 https://www.devze.com 2023-03-01 15:08 出处:网络
I was trying to understand some of the internals of Rails Relations, in order to use them effectively i开发者_如何转开发n queries. Therefore, I opened up a console and did some tests:

I was trying to understand some of the internals of Rails Relations, in order to use them effectively i开发者_如何转开发n queries. Therefore, I opened up a console and did some tests:

ruby-1.9.2-p180 :036 > Skill.where(:second_class => 'Wealth Ranger').map {|att| att.class}
 => [Skill(id: integer, name: string, description: string, second_class: string, third_class: string, created_at: datetime, updated_at: datetime)]

Now, I would not expect that output. I would expect something like Relation or something similar. But it seems like it traverses every attribute and produces the type for each one.

What is a Relation in Ruby's terms really? Is it a totally custom structure? If so, are there similarities between hashes and arrays, or should it be considered like a totally custom structure?

EDIT:

After some more testing, it seems like it just contains objects of that class. Is that how it operates?


ActiveRecord's Relation class in Rails 3 is really just a layer on top of Arel. It handles collecting parameters for "lazy loading" and Rails' simplified query methods (compared to straight Arel).

The best description I've seen of it's inner workings is on Ernie Miller's blog. He wrote meta_search and meta_where so he knows what he's talking about.

0

精彩评论

暂无评论...
验证码 换一张
取 消