Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

How does Rails ActiveRecord chain "where" clauses without multiple queries?

I'm a PHP developer learning the awesomness of Ruby on Rails, I'm loving ActiveRecord and i noticed something really interesting, Which is how ActiveRecord methods detect the end of method chain to execute the query.

@person = Person.where(name: 'Jason').where(age: 26)

# In my humble imagination I'd think that each where() executes a database query
# But in reality, it doesn't until the last method in the chain

How does this sorcery work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The where method returns an ActiveRecord::Relation object, and by itself this object does not issue a database query. It's where you use this object that matters.

In the console, you're probably doing this:

@person = Person.where(name: "Jason")

And then blammo it issues a database query and returns what appears to be an array of everyone named Jason. Yay, Active Record!

But then you do something like this:

@person = Person.where(name: "Jason").where(age: 26)

And then that issues another query, but this one's for people who are called Jason who are 26. But it's only issuing one query, so where'd the other query go?


As others have suggested, this is happening because the where method returns a proxy object. It doesn't actually perform a query and return a dataset unless it's asked to do that.

When you run anything in the console, it's going to output the inspected version of the outcome of whatever it is you ran. If you put 1 in the console and hit enter, you'll get 1 back because 1.inspect is 1. Magic! Same goes for "1". A variety of other objects don't have an inspect method defined and so Ruby falls back to the one on Object which returns something ghastly like <Object#23adbf42560>.

Every single ActiveRecord::Relation object has the inspect method defined on it so that it causes a query. When you write the query in your console, IRB will call inspect on the return value from that query and output something almost human readable, like the Array that you'd see.


If you were just issuing this in a standard Ruby script, then no query would be executed until the object was inspected (via inspect) or was iterated through using each, or had the to_a method called on it.

Up until one of those three things happen, you can chain as many where statements on it as you will like and then when you do call inspect, to_a or each on it, then it will finally execute that query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...