add scoped finders
Reported by Grant Rodgers | September 6th, 2007 @ 04:55 PM
It would be really cool if Ambition had scoped finders, ala scope_out, has_finder, and scoped_proxy
Combining nested scopes with kicker methods would do wonders I think:
Article.published.popular.first
select * from articles where (published = 1) AND (popular = 1) LIMIT 1
Comments and changes to this ticket
-
Chris Wanstrath September 16th, 2007 @ 12:36 PM
- State changed from new to invalid
The plan is for Ambition to only support the Ruby Enumerable API. You're welcome to write an add-on which supports this, though.
-
Grant Rodgers September 20th, 2007 @ 06:40 PM
went ahead and wrote this. Rather small amount of code.
-
Grant Rodgers September 20th, 2007 @ 06:40 PM
- no changes were found...
-
Grant Rodgers September 20th, 2007 @ 06:41 PM
- no changes were found...
-
Grant Rodgers September 20th, 2007 @ 06:41 PM
- no changes were found...
-
Grant Rodgers September 20th, 2007 @ 06:46 PM
works like this:
class User < ActiveRecord::Base scoped_context :admins do |context| context.select { |u| u.type == 'Admin' } end end User.admins.to_sql #=> "SELECT * FROM users WHERE users.type = 'Admin'"
-
Grant Rodgers September 20th, 2007 @ 06:47 PM
Not sure how add-ons are packaged, unfortunately - let me know if there's something else to be done.
-
Chris Wanstrath September 21st, 2007 @ 11:59 AM
You can do this already, y'know:
class Company def self.high_rated query_context.select { |c| c.rating > 3 } end end >> Company.high_rated.to_sql => "SELECT * FROM companies WHERE companies.`rating` > 3" >> Company.high_rated.first => #<Company:0x26eb1ec ...>
-
Grant Rodgers September 21st, 2007 @ 12:06 PM
Yep, that occurred to me. Certainly a matter of taste which you prefer.
I think it might be a bit better encapsulated as a special method, and maybe faster since it doesn't go through the query processor for every call.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป