Questions tagged [named-scope]
339 questions
2
votes
3 answers
Rails 2.3.8 named_scope chaining
I have the following nested if statement hairball, I'm wondering if there is a more efficient way of writing this code (less lines of code that doesn't require so many conditionals)
Each of the methods are named_scopes within the model..
box =…

Rabbott
- 4,282
- 1
- 30
- 53
2
votes
3 answers
Rails3 - many_to_many relationships and scope chaining
Let's say I have a many_to_many relation ship between Articles and Tags
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, :through =>…

MrRuru
- 1,932
- 2
- 20
- 24
2
votes
3 answers
Rails 3: How to create a named scope based on Controller's method?
In my ApplicationController I have the demo_mode? method (which returns true when the currently logged in user type is "demo").
Post model has the publisher_id field, which refers to Users table.
User has the user_type field, one of the possible…

Misha Moroshko
- 166,356
- 226
- 505
- 746
2
votes
3 answers
Iterating over a has_many collection within a named_scope
Here are my models:
class Message < ActiveRecord::Base
has_many :comments
attr_accessible :read #bool
def unread_comments?
comments.each { |comment| return true unless comment.read?}
false
end
end
class Comment <…

TheDelChop
- 7,938
- 4
- 48
- 70
2
votes
2 answers
Will this work with named_scope in rails?
I can't find the answer to this anywhere, and I don't have the brainpower left today to think up a way to confirm it on my own.
I have a named scope like this...
named_scope :fresh, :conditions => ['updated_at > ?', 4.hours.ago]
And I don't know if…

anithri
- 340
- 1
- 9
2
votes
3 answers
ActiveRecord keeping scope encapsulated
I have two models, foo and bar, foo has many bars.
Bar is an event that happens for a given period of time, so I'd like a method or scope that returns an ActiveRecord::Relation representing the foos that have currently active bars.
This is easy…

SooDesuNe
- 9,880
- 10
- 57
- 91
2
votes
1 answer
Rails 3: How to merge queries or scopes for complex query?
I'm building an events app that is very simple, it has a title and start_date and end_date. I would like to filter my query by mixing some of the values, like: if the start_date has passed but the end_date has not, the event is active and should be…

Landitus
- 1,890
- 4
- 23
- 27
2
votes
1 answer
How would I join to a subselect (a scope) using Rails 3 and Arel?
I need to join a table to the a select/group-by query (which includes the same table), and I'd like to do it using Arel.
I have a table of :phenotypes which are has_and_belongs_to_many :genes, which are themselves has_and_belongs_to_many…

Translunar
- 3,739
- 33
- 55
2
votes
1 answer
How to allow JS script to use certain variables
I'm trying to make game, where you have to program AI for your bot. I know you can access variable from your code with whole map window.myGame.data.map and you can modify it, for example, erase other bots.
How can I prevent this? Is there sandbox…

Jan Kaifer
- 664
- 4
- 18
2
votes
2 answers
Is there a build in functionality in .NET to create thread bound variables?
Is there a way to do this (psedo code):
GetCurrentThread().Items.Add(new RefObject);
then later on retrive it
RefObject[] refObjs = GetCurrentThread().Items;
and enumerate the objects. Obviously both calls will occur on the same thread during the…

user44298
- 910
- 7
- 20
2
votes
1 answer
Rails ActiveRecord 4.2 custom sort order
I'm trying to apply a custom scope or class method to the following ActiveRecord model however I'm not sure of the best approach or implementation following Rails best practices.
Please note this is just a simplified example project for explanation…

Charles Green
- 413
- 3
- 15
2
votes
2 answers
How to add existing named_scope to anonymous scope?
I have a model:
class Shirt < ActiveRecord::Base
named_scope :red, :conditions => { :color => 'red' }
named_scope :blue, :conditions => { :color => 'blue' }
named_scope :xl, :conditions => { :size => 'xl' }
end
I forgot, how to easy add…

Sławosz
- 11,187
- 15
- 73
- 106
2
votes
1 answer
Default conditions for Rails models
I have a model which has a field called deleted, which is used to mark those deleted items.
So normally I would just want to query those having deleted = false items, and in some special cases to list those deleted items for restoring.
Is it…

PeterWong
- 15,951
- 9
- 59
- 68
2
votes
1 answer
What is a difference between named_scope and named_scope + lambda
What is a difference between named_scope and named_scope + lambda Ruby on Rails code statements?
named_scope :with_avatar, :conditions => ['avatar IS NOT NULL']
and
named_scope :date_from, lambda { |date| { :conditions => ['created_at >= ?',…

Maestro
- 75
- 1
- 7
2
votes
2 answers
Error when combining search scopes
I have a web-service that allows clients to search for articles with query parameters.It works fine if only one parameter is included but fails if I combine search_query and category. This is based on Comfortable_Mexican_Sofa where for_category is…

Antarr Byrd
- 24,863
- 33
- 100
- 188