Questions tagged [named-scope]
339 questions
113
votes
4 answers
What is scope/named_scope in rails?
I've recently started an internship. My employer uses ruby on rails, and I frequently encounter new syntax that I need to look up to understand. I've googled around for a good explanation of named_scope, but what I've found so far is mostly blog…

Ziggy
- 21,845
- 28
- 75
- 104
59
votes
6 answers
How do you scope ActiveRecord associations in Rails 3?
I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many").
I have records which have permission columns. …

Mario
- 6,572
- 3
- 42
- 74
45
votes
9 answers
Is it possible to negate a scope in Rails?
I have the following scope for my class called Collection:
scope :with_missing_coins, joins(:coins).where("coins.is_missing = ?", true)
I can run Collection.with_missing_coins.count and get a result back -- it works great!
Currently, if I want to…

Yuval Karmi
- 26,277
- 39
- 124
- 175
30
votes
1 answer
Rails –Testing named scopes: test scope results or scope configuration?
How should Rails named scopes be tested? Do you test the results returned from a scope, or that your query is configured correctly?
If I have a User class with an .admins method like:
class User < ActiveRecord::Base
def self.admins
…

Paul Fioravanti
- 16,423
- 7
- 71
- 122
28
votes
3 answers
Rails: Is that possible to define named scope in a module?
Say there are 3 models: A, B, and C. Each of these models has the x attribute.
Is that possible to define a named scope in a module and include this module in A, B, and C ?
I tried to do so and got an error message saying that scope is not…

Misha Moroshko
- 166,356
- 226
- 505
- 746
20
votes
2 answers
Rails named_scopes with joins
I'm trying to create a named_scope that uses a join, but although the generated SQL looks right, the result are garbage. For example:
class Clip < ActiveRecord::Base
named_scope :visible, {
:joins => "INNER JOIN series ON series.id =…

Gwyn Morfey
- 32,741
- 6
- 25
- 21
18
votes
1 answer
Mongoid Scope order by syntax please
I'm using the latest mongoid...
How do I do the mongoid equivalent of this active record named_scope:
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :post
field :body, :type => String
named_scope…

kinet
- 1,790
- 3
- 20
- 32
18
votes
7 answers
Empty Scope with Ruby on Rails
Following Problem:
I need something like an empty scope. Which means that this scope is emtpy, but responds to all methods a scope usually responds to.
I'm currently using a little dirty hack. I simply supply "1=0" as conditions. I find this realy…

Baju
- 2,616
- 1
- 19
- 29
17
votes
5 answers
Can I use an ActiveRecord scope as an instance method?
I have a scope that acts as a filter. For instance:
class User
scope :in_good_standing, -> { where(:some_val => 'foo', :some_other_val => 'bar' }
end
Because in_good_standing depends on more than one condition, I'd like to have this defined on an…

JD Huntington
- 268
- 1
- 2
- 7
17
votes
2 answers
undefined method `default_scoped?' while accessing scope
I am getting this error while accessing scopes.
Here is AR model
class StatisticVariable < ActiveRecord::Base
attr_accessible :code, :name
has_many :statistic_values
scope :logins, where(code: 'logins').first
scope :unique_logins,…

Amit Patel
- 15,609
- 18
- 68
- 106
16
votes
4 answers
will_paginate with named_scopes
I'm using will_paginate for pagination, which has been working well so far, except for this one thing.
If I try to paginate a scope, for instance
class User < ActiveRecord::Base
named_scope :scope, lambda { etc…

Lowgain
- 3,254
- 5
- 27
- 30
15
votes
2 answers
Does testing Rails 3 Scopes make sense?
I'm a little torn. Do unit tests for Scopes in Rails 3 make sense?
On the one hand, I'm writing code and I should test that code.
However, on the other hand, basically all my scopes are effectively trivial. Checking one variable against a passed…

Drew
- 15,158
- 17
- 68
- 77
12
votes
2 answers
Confused using "using" Statement C#
According to MSDN Library
using Statement (C# Reference)
Defines a scope, outside of which an object or objects will be disposed.
But I got this code posted here by some user and I got confused about this: (please see my comment on the code)
using…

yonan2236
- 13,371
- 33
- 95
- 141
12
votes
1 answer
counter_cache in Rails on a scoped association
I have User model which has_many :notifications. Notification has a boolean column seen and a scope called :unseen which returns all notifications where seen is false.
class User < ApplicationRecord
has_many :notifications
has_many…

GMA
- 5,816
- 6
- 51
- 80
12
votes
2 answers
How to extract common named_scopes from ActiveRecord models
I have named_scope which is reused in multiple ActiveRecord models.
For example:
named_scope :limit, lambda {|limit| {:limit => limit}}
What is the best practice to extract this code to be shared across models.
Is it possible to extract it to…

Bartosz Blimke
- 6,498
- 3
- 24
- 19