Questions tagged [named-scope]
339 questions
3
votes
3 answers
Why does this Rails named scope return empty (uninitialized?) objects?
In a Rails app, I have a model, Machine, that contains the following named scope:
named_scope :needs_updates, lambda {
{ :select => self.column_names.collect{|c| "\"machines\".\"#{c}\""}.join(','),
:group => self.column_names.collect{|c|…

mipadi
- 398,885
- 90
- 523
- 479
3
votes
1 answer
Custom ActiveRecord finder invoking named scopes?
I have a custom finder defined below:
class ContainerGateIn << ActiveRecord::Base
...
def self.search(text)
result = if text
text.split(' ').inject(self) do |result, criteria|
case criteria
when…

Erol
- 370
- 1
- 3
- 10
3
votes
4 answers
Can I access the id of the parent object inside a named_scope when fetching associated objects with the 'others' method?
Let's say you have two models: articles and comments.
class Article < ActiveRecord::Base
has_many :comments
end
You know you can fetch associated comments to an article like this:
article = Article.first
article.comments # => SELECT * FROM…

Daniel Pietzsch
- 1,369
- 14
- 21
3
votes
2 answers
named_scope to order posts by last comment date
Posts has_many Comments
I'm using searchlogic which will order by named scopes. So, I'd like a named scope that orders by each post's most recent comment.
named_scope :ascend_by_comment, :order => ...comments.created_at??...
I'm not sure how to do…

wesgarrison
- 6,975
- 5
- 30
- 39
3
votes
1 answer
default_scope with :joins and :select
I tried to define a default_scope in the following way:
default_scope :joins => :product, :select => "catalog_products.*, products.*"
What I'm getting from Rails though is this:
SELECT catalog_products.* FROM `catalog_products` INNER JOIN…

Milan Novota
- 15,506
- 7
- 54
- 62
3
votes
1 answer
How do I re-use named scopes?
Hi I have a named_scope in my User model as following.
named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } }
I want to create other two named scopes which re use this one something like,
named_scope :male, lambda {…

Waseem
- 8,232
- 9
- 43
- 54
3
votes
1 answer
ActiveRecord named_scope, .scopes
The background to this problem is quite complex and convoluted, and as I am looking for a simple answer, I will leave it by the wayside in explaining my problem, and instead provide this hypothetical situation.
If I have a simple ActiveRecord model…

Synthlabs
- 138
- 5
3
votes
3 answers
Matching available languages to language names
I want to make a language selection dropdown in a site user edit/create page.
For this purpose, I have of course translated the site to more than one language.
Using I18n.available_languages, I can then get an array of locale codes, like…

HansCz
- 284
- 1
- 6
- 11
3
votes
1 answer
Rails: Can joins be merged when chaining scopes?
In a class A I have two scopes, s1 and s2 which both join over a table T using the exact same join columns:
named_scope :s1 :joins => "JOIN T on T.id = A.t_id", ...some conditions
named_scope :s2 :joins => "JOIN T on T.id = A.t_id", ...some other…

mxk
- 43,056
- 28
- 105
- 132
3
votes
1 answer
NInject services needed in multiple named scopes
I made a post on the Ninject forum but haven't received any answers... wondering if anyone has any suggestions?
http://groups.google.com/group/ninject/browse_thread/thread/9ac79d5541f015cb
Hello everyone,
I have a few different workflows in a…

schaibaa
- 71
- 2
2
votes
1 answer
Best way to scope a has_many relationship when find()'ing the parent object
I'm struggling a bit with understanding default scopes and named scopes with my quiz application. I'm on rails 3.0.
I have a Question model that has_many UserResponse models.
Question has the question text and possible answer_ids. UserResponse…

1locoburro
- 23
- 3
2
votes
0 answers
How do I stub a named_scope so .find can still be called on the resultset?
I'm mantaining a Rails app.
Controller old code:
@articles = Articles.unpublished.find(complicated_conditions)
I'm changing it to something like this:
if logged_user.has_a_specific_permission
@articles =…

dipnlik
- 378
- 4
- 13
2
votes
1 answer
Why doesn't #is_a? work with an instance of ActiveRecord::NamedScope::Scope
I'm using Rails 2.3.9. To illustrate the issue, suppose we have a named scope on a model:
class Book < ActiveRecord::Base
named_scope :has_isbn, :conditions => 'isbn IS NOT NULL'
end
If you get the class of the named scope,…

Claw
- 767
- 8
- 22
2
votes
1 answer
Problem with named_scope causes error in will_paginate - how to include group_by in count?
[rails 2.3.12] named_scope:
named_scope :order_by_price, lambda {{:joins => :variants, :group => "products.id", :order => "MAX(price)"}}
console:
1.
> Product.order_by_price.size
=> 21
2.
> p = Product.order_by_price
> p.size
=> 4
sql…

santuxus
- 3,662
- 1
- 29
- 35
2
votes
1 answer
Converting method to scope in Rails 3
I'd like to convert this method to a scope in rails so I could call something like Batch.all_completed and it would return all batches that met the criteria in the method:
def all_completed?
articles.with_status('Completed').count >=…

Dave G.
- 23
- 3