named-scope

Converting a simple query into a tricky named scope in RoR

霸气de小男生 提交于 2020-01-25 04:43:14
问题 I have a simple scoping question. I would like to do this as a scope: if article.responses.blank? return false elsif article.responses.last.passed.eql?(false) return true else return false end So on the article model I would have something like this: scope :failed_response, { :joins=>[:responses], :conditions=>["responses.passed = ?", false] } The problem is, I only want instances where the most recent response failed. I'm sure these is a way to do this with either fancy sorting or some kind

Rails Testing a named_scope with a date range

元气小坏坏 提交于 2020-01-14 06:29:53
问题 SCENARIO I have a named_scope on a model called 'last_week'. All it does is it fetches the records from last week. I want to test this and my approach is to test that the results coming back are within a certain range. I don't know if this is the right way of testing this functionality on a class method. I can't use RSpec, Shoulda or other 3rd party client plugin. I am only allowed to use Mocha if I want. # Model class Article < ActiveRecord::Base scope :last_week, :conditions => { :created

Joins across multiple tables with ActiveRecord with named scopes

孤者浪人 提交于 2020-01-14 05:12:06
问题 I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so: named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'" Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something like this: named_scope :foo, :joins => (:bar => :baz), :conditions => "bar.id = baz.bar_id AND baz_attribute = 'something_else'" How possible is this? thanks 回答1:

Problem with named_scope causes error in will_paginate - how to include group_by in count?

与世无争的帅哥 提交于 2020-01-04 08:27:11
问题 [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 queries: 1. SELECT count(*) AS count_all FROM `products` INNER JOIN `variants` ON variants.product_id = products.id 2. SELECT `products`.* FROM `products` INNER JOIN `variants` ON variants.product_id = products.id GROUP BY products.id ORDER BY MAX(price) I use

Best way to test named scopes in Rails4

风格不统一 提交于 2019-12-25 02:40:58
问题 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 biting me in production after my migration. So I wanted to figure out how to test this issue, and I found some strange behavior. In some cases the scopes appear to work fine without the proc, but not in other cases. # models/offer.rb class Offer <

will paginate miscounting oddness with named scopes

谁说胖子不能爱 提交于 2019-12-22 08:13:27
问题 I recently broke up my query into 4 named scopes to make it easier to re-sort and will paginate which otherwise always worked fine now has problems calculating the number of pages. named_scope :loc, lambda { |id| { :conditions => ['location_id = ?', id ] } } named_scope :datem, lambda { |*args| { :joins => :scannables, :conditions => [ "scannables.bookdate BETWEEN ? and ?", (args[0].to_date || 3.days.from_now), (args[0].to_date+(args[1] || 3)) ], :group => 'scannables.hostel_id', :having =>

Rails 3: How to create a named scope based on Controller's method?

痞子三分冷 提交于 2019-12-22 01:15:49
问题 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 values of which is "demo". Bottom line: post p was published by a "demo" user if and only if: User.find(p.publisher_id).user_type == "demo" I would like to create a named scope Post.relevant that will return: all posts that were published by "demo"

Is it possible to negate a scope in Rails 3?

那年仲夏 提交于 2019-12-21 06:48:14
问题 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 get collections without missing coins, I add another scope: scope :without_missing_coins, joins(:coins).where("coins.is_missing = ?", false) I find myself writing a lot of these "opposite" scopes. Is it possible to get the opposite of a scope

How do you scope ActiveRecord associations in Rails 3?

风格不统一 提交于 2019-12-20 08:01:34
问题 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. I would like to build a default_scope that takes my permission columns into consideration so that records (even those accessed through a relationship) are filtered. Presently, in Rails 3, default_scope (including patches I've found) don't provide a

How to implement an [GoF]-ish Abstract Factory Pattern using an IoC like Ninject

时光总嘲笑我的痴心妄想 提交于 2019-12-18 07:06:32
问题 Abstract When the design requires an "Abstract Factory Pattern" like stated by the [GoF] including several products and over some product families, then setting up an IoC can become a bit tricky. Especially when the specific factory implementations need to be dispatched by runtime parameters and shared among some subsequent components. Given the follwing API, i was trying to set up my IoC (Ninject in this case) to retrieve Configuration objects configured through a IConfigurationFactory . The