Questions tagged [named-scope]

339 questions
0
votes
2 answers

Dynamically define scopes from within class << self

How can I assign scopes dynamically from within a class << self context? class Partner < ActiveRecord::Base STATUS = { pending: 0, # 0 account has a pending billing request (but is not yet open) active: 1, # 1 account has an active…
Volte
  • 1,905
  • 18
  • 25
0
votes
1 answer

Best way to test named scopes in Rails4

As a part of the migration from Rails 3.2 to Rails 4, all named scopes need a proc block. Read more here: http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#active-record I had missed updating a scope in one of my models, which ended up…
Paul Pettengill
  • 4,843
  • 1
  • 29
  • 33
0
votes
1 answer

How to delete objects through a named scope?

There is a join table with three columns: id, product_a_id, product_b_id class ProductConnection < ActiveRecord::Base belongs_to :product_a, class_name: :Product belongs_to :product_b, class_name: :Product end I would like to filter the…
JJD
  • 50,076
  • 60
  • 203
  • 339
0
votes
1 answer

named scope vs. find_by_sql (specific example)

Just out of curiosity, does anyone know a better way of building the following collection using named scopes (as opposed to find_by_sql)? @available = Workflow.find_by_sql([" SELECT workflows.id FROM workflows WHERE workflows.project_id…
Alex B.
  • 590
  • 4
  • 17
0
votes
1 answer

Rails and Postgresql how to sort with custom count? ORDER BY is ambiguous

Relationship User has_many :kits User model and named scope: scope :top5_users, joins(:kits). select("users.*, count(kits.id) AS kits_count"). group("users.id, kits.id"). order("kits_count DESC"). limit(5) Getting PG::Error:…
Jakub Kuchar
  • 1,665
  • 2
  • 23
  • 39
0
votes
1 answer

"Section" has_many versioned "Articles" -- How can I get the freshest subset?

I have a Model called Section which has many articles (Article). These articles are versioned (a column named version stores their version no.) and I want the freshest to be retrieved. The SQL query which would retrieve all articles from section_id…
John Doe
  • 13
  • 4
0
votes
1 answer

In Ruby, how to write an ActiveRecord named scope that finds the root nodes of a reflexive hierarchy?

I have defined an ItemType as follows: class ItemType < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name has_many :items has_many :children, :class_name => 'ItemType', :foreign_key => :parent_id belongs_to…
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
0
votes
3 answers

Rspec, Model load order, fixtures and named_scope challenge

I have some players and the players have a trade state. Rather than hard code trade states like "active" and "inactive" and then have to go looking for strings, I thought I'd be clever and have a separate TradeState model so that a Player has a…
Peter Degen-Portnoy
  • 786
  • 1
  • 8
  • 16
0
votes
1 answer

rails convert class methods to named scope

Rails newbie here. I'm trying to get some class methods into named_scopes. My application structure is similar to a blog application with user comments. Each comment model has a score attribute determined by ratings made from other users. I want to…
Kenji Crosland
  • 2,914
  • 6
  • 31
  • 40
0
votes
1 answer

Rails: Named scope works on Class but not instance of Class

Given the following code: user_decorator.rb Refinery::User.class_eval do has_many :employees, :class_name => 'Refinery::Employee' scope :active_employees, lambda { joins(:employees).merge(::Refinery::Employee.active) …
aarona
  • 35,986
  • 41
  • 138
  • 186
0
votes
1 answer

How to test named_scopes and search methods?

When I learned about proxy_options I started using it to test all my named scopes. But then I found myself simply copying the conditions hash straight from the model, so it wasn't really testing the correctness of the results: po = {:conditions =>…
eremite
  • 1,886
  • 15
  • 18
0
votes
2 answers

Rails: select in an intricate relationship

I am rather new to rails, so I ask for your patience. I have a bit of an intricate relationship between three (ActiveRecord) models: class Producer has_many :clients end class Client belongs_to :producer has_many :products end class…
0
votes
3 answers

rails named scope issues

I have two named scopes... both which work separately, but when combined do not work. named_scope :total, :select => "COUNT(*) as days, AVG(price) as price, SUM(price) AS total", :group => :parent_id named_scope :currency, lambda { |code| {…
holden
  • 13,471
  • 22
  • 98
  • 160
0
votes
2 answers

basic modification of default model output with scope

I find myself doing the same things over and over again just to make one small modification to standard model output. I have a series of tables that I store information about products, etc. and all of which store prices. The prices are stored in…
holden
  • 13,471
  • 22
  • 98
  • 160
0
votes
1 answer

Rails Complex Query Suggesting Next Object

I have a complex scope on my model giving the user active deals that meet their parameters. scope :deal_query, lambda { |m, p| where("meal = ? and active = ? and people LIKE '%?%' and inactive_days NOT LIKE '%?%'", m, true, p,…