For an instance of ActiveRecord::Relation, is there a method that would return all the conditions/scopes used?
This 开发者_开发问答question applies for Rails 3.x
Let's say I have a Business model, and Company and City models...each Business belongs to a company and belongs to a City...and I have scopes set up so that:
City.businesses.owned_by(some_company_id)
...returns all businesses in that city, owned by some_company.
Is there a method that I can use at the end of that chain of scopes to return all the conditions used to return the relevant activerecords?
By calling arel.constraints
or just constraints
on the ActiveRecord::Relation
you have access to the conditions array.
For example, in one of my applications I have a scope called active
:
z = Auction.active.constraints.last
=> #<Arel::Nodes::Grouping:0x007fa66753c108 @expr="auctions.ending_at > '2011-08-22 23:44:11.591438'">
z.expr
=> "auctions.ending_at > '2011-08-22 23:44:14.295435'"
精彩评论