Questions tagged [named-scopes]
17 questions
28
votes
1 answer
Rails scope vs named_scope
Is there any difference between scope and named_scope?

methyl
- 3,272
- 2
- 25
- 34
24
votes
3 answers
Can you alias a scope in Rails?
Say I have this scope:
scope :with_zipcode, lambda { |zip| where(zipcode: zip) }
and I want an equivalent scope
scope :has_zipcode, lambda { |zip| where(zipcode: zip) }
is there a way to alias one scope to another? For instance something…

msquitieri
- 332
- 2
- 10
6
votes
2 answers
Why are Rails scopes preferable, if messy controllers are faster?
I've been trying to chain Arel queries using scopes, instead of just using some long-winded logic I wrote in the controller. But the scopes are slower than just getting all the records and then sifting through them with some logic. I'm wondering,…

Steve Cotner
- 505
- 4
- 14
3
votes
2 answers
Rails scopes with multiple arguments
In my offer.rb model I am doing some filtering after clients who have these offers.
But I want to be able to pass another param in my scope to search by, for example, the age of a client or so.
This is what I have now in my offer model:
scope…

Bogdan Popa
- 1,099
- 1
- 16
- 37
2
votes
1 answer
Can validate_uniqueness_of work with custom scopes?
I'm working on an RoR project and I'd like to have a uniqueness validation on one of my models that checks against a custom scope:
class Keyword < ActiveRecord::Base
belongs_to :keyword_list
scope :active, -> { where("expiration > ?",…

Dylan Karr
- 3,304
- 4
- 19
- 29
1
vote
1 answer
Scoping f.collection_select
I've got a model that I'm using a collection_select on and I'd like to scope it to show only accounts that are open. To accomplish this, I've added a boolean field to my account model, defaulting to false.
Here's my attempt at that in my account…

PSCampbell
- 858
- 9
- 27
1
vote
1 answer
Access named scope dynamically
If I have 3 named scopes like
class Foo
scope :test1, ...
scope :test2, ...
scope :test3, ...
And a function
def x(variable)
end
where variable is a string("test1","test2" or "test3")
How can I access the named scope just by knowing that…

vladCovaliov
- 4,333
- 2
- 43
- 58
0
votes
2 answers
Nested Rails Joins within Scopes
I've got 3 classes. Country, City, and Activity. A country has_many cities and a city has_many activities.
Some of the activities aren't ready yet, so the activities have an approval status. I'd like to set it up scopes, so a city without approved…

jacob.hanshaw
- 1
- 2
0
votes
1 answer
List results by scope where (field) not nil THEN second condition
I'm wondering if there's a way to make one query to get the following conditions:
1) scope :scheduled_for, -> {where.not(:scheduled_for => nil)}
then
2) scope :sort_by_position, -> {order('position')}
The expected result would be the following…

Vincent
- 147
- 10
0
votes
1 answer
RubyOnRails multiple scopes composition
I got a Product model with has_many Types table and several scopes:
class Product < ActiveRecord::Base
has_many :product_types
has_many :types, through: :product_types
scope :type1, -> { joins(:types).where(types: { name: "Type1" }) }
…

Anton Khaybulaev
- 89
- 1
- 8
0
votes
0 answers
Rails - routes and scopes
I am trying to make an app with Rails 4.
I have 4 relevant models for this question, being Project, Scope, Disclosure, Finalise.
The associations are:
Project.rb:
has_one :scope
accepts_nested_attributes_for :scope
In my project.rb model, I have…

Mel
- 2,481
- 26
- 113
- 273
0
votes
2 answers
activerecord setting in scope
I have a table business_settings that uses key and value columns to store the settings for a business.
I have written a helper to gather these values:
def bus_setting(bus_key)
bus_setting = BusinessSetting.where(key: bus_key).first
return…

jared
- 231
- 3
- 13
0
votes
1 answer
Rails : scope for has_many association
I have 2 models Person and PersonInterest :
People contains all the information about a person
Person Interests stores the interests of a person (person_id, interest_id)
Person has_many PersonInterests
How would I create a scope that can search…

Michael Victor
- 861
- 2
- 18
- 42
0
votes
0 answers
Thinking Sphinx OR on multiple columns within scope
I need a way of conditioning a scope so that it checks if a value exists in one of three columns. This is my not code, I have just been told to edit it so it's hard to understand.
So, my condition in MYSQL would be:
WHERE `sub_client_1_id` = 53 OR…

Stefan Dunn
- 5,363
- 7
- 48
- 84
0
votes
0 answers
Rails: Better understanding dynamic scopes
This is my "dynamic" scope:
def all_games(conditions = {})
scope = games.includes(:stats).scoped {}
scope = scope.where sport_position_id: conditions[:sport_position_id] unless conditions[:sport_position_id].nil?
scope = scope.where…

dennismonsewicz
- 25,132
- 33
- 116
- 189