named-scope

Is there a way to combine named scopes into a new named scope?

纵饮孤独 提交于 2019-11-30 20:41:37
I have class Foo < ActiveRecord::Base named_scope :a, lambda { |a| :conditions => { :a => a } } named_scope :b, lambda { |b| :conditions => { :b => b } } end I'd like class Foo < ActiveRecord::Base named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } } end but I'd prefer to do it in a DRY fashion. I can get the same effect by using Foo.a(something).b(something_else) but it's not particularly lovely. Well I'm still new to rails and I'm not sure exactly what you're going for here, but if you're just going for code reuse why not use a regular class method? def self.ab(a, b) a(a).b

Is there a way to combine named scopes into a new named scope?

心不动则不痛 提交于 2019-11-30 04:07:44
问题 I have class Foo < ActiveRecord::Base named_scope :a, lambda { |a| :conditions => { :a => a } } named_scope :b, lambda { |b| :conditions => { :b => b } } end I'd like class Foo < ActiveRecord::Base named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } } end but I'd prefer to do it in a DRY fashion. I can get the same effect by using Foo.a(something).b(something_else) but it's not particularly lovely. 回答1: Well I'm still new to rails and I'm not sure exactly what you're going for

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

老子叫甜甜 提交于 2019-11-29 12:07:49
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 configuration stores an IFactory instance whoose implementation is determined by a runtime parameter

How to have multiple conditions in a named scope?

十年热恋 提交于 2019-11-29 11:06:05
I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?) . I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have: scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } } This successfully gets all Users updated within the last 5 minutes, but how do I incorporate the admin check? I don't know how to call try() on an instance of a User inside the scope... if admin column in users table is a boolean, scope :recent, lambda { :conditions => ['updated_at > ? AND admin != ?', 5

undefined method `default_scoped?' while accessing scope

核能气质少年 提交于 2019-11-29 09:13:02
I am getting this error while accessing scopes. Here is AR model class StatisticVariable < ActiveRecord::Base attr_accessible :code, :name has_many :statistic_values scope :logins, where(code: 'logins').first scope :unique_logins, where(code: 'unique_logins').first scope :registrations, where(code: 'registrations').first end and when I try with StatisticVariable.logins or any other scopes it gives: NoMethodError: undefined method `default_scoped?' If I configure scope as class method then it works perfectly. def self.registrations where(code: 'registrations').first end Please guide me to

Using named_scope with counts of child models

坚强是说给别人听的谎言 提交于 2019-11-29 02:38:00
I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars named_scope :with_no_bars, ... # count of bars == 0 named_scope :with_one_bar, ... # count of bars == 1 named_scope :with_more_than_one_bar, ... # count of bars > 1 end class Bar < ActiveRecord::Base belongs_to :foo end I'm hoping to do something like Foo.with_one_bar I could write methods on the parent class for something like this, but I'd rather have the power of

Rails: Is that possible to define named scope in a module?

爷,独闯天下 提交于 2019-11-28 18:41:14
Say there are 3 models: A, B, and C. Each of these models has the x attribute. Is that possible to define a named scope in a module and include this module in A, B, and C ? I tried to do so and got an error message saying that scope is not recognized... Yes it is module Foo def self.included(base) base.class_eval do scope :your_scope, lambda {} end end end As of Rails 3.1 the syntax is simplified a little by ActiveSupport::Concern: Now you can do require 'active_support/concern' module M extend ActiveSupport::Concern included do scope :disabled, where(:disabled => true) end module ClassMethods

Combining multiple named scopes with OR

浪子不回头ぞ 提交于 2019-11-28 14:08:15
I am trying to combine two scopes or add some more to an existing scope. scope :public_bids, -> { where(status: [Status::ACCEPTED, Status::OUTBID, Status::SOLD, Status::NO_SALE], bid_type: [BidType::MANUAL, BidType::AUTO, BidType::LIVE]) scope :outbid_maxbids, -> { where(status: Status::OUTBID, bid_type: BidType::MAXBID) I am having trouble determining how to OR them together or combine then into one scope. Any suggestions/guidance? I would prefer that they be combined into one single scope. Uzbekjon Rails 4 and older do not support OR query "natively". Looks like it will be introduced in

undefined method `default_scoped?' while accessing scope

我怕爱的太早我们不能终老 提交于 2019-11-28 02:37:59
问题 I am getting this error while accessing scopes. Here is AR model class StatisticVariable < ActiveRecord::Base attr_accessible :code, :name has_many :statistic_values scope :logins, where(code: 'logins').first scope :unique_logins, where(code: 'unique_logins').first scope :registrations, where(code: 'registrations').first end and when I try with StatisticVariable.logins or any other scopes it gives: NoMethodError: undefined method `default_scoped?' If I configure scope as class method then it

How to have multiple conditions in a named scope?

主宰稳场 提交于 2019-11-28 00:51:07
问题 I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?) . I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have: scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } } This successfully gets all Users updated within the last 5 minutes, but how do I incorporate the admin check? I don't know how to call try() on an instance of a User inside the scope... 回答1: if admin