I have naming question.
Suppose if we have these kind of scopes in model of Rails3 application,
scope :order_by_date, order("created_at asc")
scope :order_by_date_desc, order("created_at desc")
The name of scope is too long and explanatory as scope.
scope :ordered, order("created_at asc")
But if we define as above, it's difficult to distinguish between asc or desc.
So how do you use scope such a case? or do you use scope?
Does anyone have good idea?
Thanks.
Well...
Model.ascending_order
Model.descending_order
Related
I have a scope in user.rb
scope :active, -some logic here-
How can I use this a where query, something like
User.where("users.active is not nil")
Is there a way to use scopes in a where clause?
Of course it is. Just like this:
User.where("users.active is not nil").active
I have a model for user.rb, in which I define a scope for admins, which is users that have the role of admin through a permissions table.
has_many :permissions
has_many :roles, :through => :permissions
The scope works like this:
scope :admins, joins(:permissions).merge(Permission.admin_permissions)
I'd also like to make a scope called non-admins or something like that, which is all users that do NOT have the admin role.
What's the easiest way to do this?
If you want to have an inverted SQL query, you will have to do it yourself manually. There is no built-in ActiveRecord solution.
scope :admins, joins(:permissions).merge(Permission.where("permissions.admin = true"))
scope :non_admins, joins(:permissions).merge(Permission.where("permissions.admin = false"))
If there are a lot of scopes or they are complex, consider excluding them by id:
User.where("id not in (?)", User.admins.pluck(:id))
# or if you are already using admin records
admins = User.admins
User.where("id not in (?)", admins.map(&:id))
Depending on number of rows and complexity of the original query, this could be slower or faster than the previous way.
An easy way, which would not be performant for a lot of users:
admin_user_ids = User.admins.map(&:id)
non_admin_users = User.all.reject { |u| admin_user_ids.include?(u.id) }
I'm finding myself writing very similar code in two places, once to define a (virtual) boolean attribute on a model, and once to define a scope to find records that match that condition. In essence,
scope :something, where(some_complex_conditions)
def something?
some_complex_conditions
end
A simple example: I'm modelling a club membership; a Member pays a Fee, which is valid only in a certain year.
class Member < ActiveRecord::Base
has_many :payments
has_many :fees, :through => :payments
scope :current, joins(:fees).merge(Fee.current)
def current?
fees.current.exists?
end
end
class Fee < ActiveRecord::Base
has_many :payments
has_many :members, :through => :payments
scope :current, where(:year => Time.now.year)
def current?
year == Time.now.year
end
end
Is there a DRYer way to write a scopes that make use of virtual attributes (or, alternatively, to determine whether a model is matched by the conditions of a scope)?
I'm pretty new to Rails so please do point out if I'm doing something stupid!
This in not an answer to the question, but your code has a bug (in case you use something similar in production): Time.now.year will return the year the server was started. You want to run this scope in a lambda to have it behave as expected.
scope :current, lambda { where(:year => Time.now.year) }
No, there's no better way to do what you're trying to do (other than to take note of Geraud's comment). In your scope you're defining a class-level filter which will generate SQL to be used in restricting the results your finders return, in the attribute you're defining an instance-level test to be run on a specific instance of this class.
Yes, the code is similar, but it's performing different functions in different contexts.
Yes, you can use one or more parameters with a lambda in your scopes. Suppose that you have a set of items, and you want to get back those that are either 'Boot' or 'Helmet' :
scope :item_type, lambda { |item_type|
where("game_items.item_type = ?", item_type )
}
You can now do game_item.item_type('Boot') to get only the boots or game_item.item_type('Helmet') to get only the helmets. The same applies in your case. You can just have a parameter in your scope, in order to check one or more conditions on the same scope, in a DRYer way.
Coming from Rails 2 to Rails 3 I've never worked so hard to understand something (side editorial).
Anyway, In a Rails 3 app i have the following models...
User:
has_many :answers
Answer:
belongs_to :user
belongs_to :question
scope :user_answers, where (:user_id => current_user.id)
Question:
has_many :answers
scope :qs_w_user_ans, joins(:questions) & (:user_answers)
The current error i am getting is "undefined method `includes_values' for :user_answers:Symbol"
There is a Question id and a User id. Each answer has question_id and user_id.
I need the questions with a user's answers linked appropriately via the ids. Can you show me where my models are wrong?
Thank you.
The & operator (which I believe is recently deprecated) is an alias for merge, which allows you to essentially merge scopes. :user_answers isn't a scope, so you can't use this method.
As Dinatih pointed out, you can call joins multiple times. In this case, creating different scopes for each join won't buy you much, so his method suits your case.
More info on scopes: http://archives.edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html
Update
Sorry for my misunderstanding. :user_answers is a scope, but you're not calling it correctly in this case. You want the following:
scope :qs_w_user_ans, joins(:questions) & Answer.user_answers
When merging scopes, you call the merged scopes like class methods.
In the article I linked, the scope :published on Post is merged with the scope :published on User:
scope :published, lambda {
joins(:posts).group("users.id") & Post.published
}
Rails 4
Question.joins(Answer.user_answers)
[ http://guides.rubyonrails.org/active_record_querying.html#joining-tables ]
joins(:questions).joins(:user_answers)
I have this model
User.rb
default_scope :order => 'users.created_at DESC'
and
scope :ranking, lambda { unscoped { order('users.ranking DESC') }}
and still I get a to_sql that includes ORDER BY users.created_at DESC, users.ranking DESC...
can someone explain why?
I really don't want to have to call unscoped from every controller i'll be using this model in.
Thanks!
As you're discovering, default_scope is often more trouble than it's worth. If you're wanting to stick with it, you could use reorder to ignore the previous order:
scope :ranking, reorder("ranking DESC")
Not sure why #TimPost deleted my answer but I'm using rails 3.0.5 and ruby 1.9.2 for a project and when I used reorder(which works btw) it says this in the log
DEPRECATION WARNING: reorder is deprecated. Please use except(:order).order(...) instead. (called from <class:Item>
So I don't think it is fair my answer was deleted and I got dinged for a crappy response