named-scope

Using named_scope with counts of child models

瘦欲@ 提交于 2019-12-18 03:45:54
问题 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

Using named_scope with counts of child models

南楼画角 提交于 2019-12-18 03:45:25
问题 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

Rails gem rails3-jquery-autocomplete how to scope by user

和自甴很熟 提交于 2019-12-17 16:31:16
问题 I'm using the Rails gem rails3-jquery-autocomplete to add categories to posts. I would like to restrict the search to include only categories that belong to the current user or post's author in the results. The documentation says that I can specify a scope: :scopes Added option to use scopes. Pass scopes in an array. e.g :scopes => [:scope1, :scope2] But I'm not sure how I would pass the user id here? It seems like a comon scenario, am I missing something obvious? I found an answer that

What is scope/named_scope in rails?

空扰寡人 提交于 2019-12-17 10:07:53
问题 I've recently started an internship. My employer uses ruby on rails, and I frequently encounter new syntax that I need to look up to understand. I've googled around for a good explanation of named_scope, but what I've found so far is mostly blog posts giving high praise for it, rather a straight definition or introduction. What exactly is named_scope (now simply called scope) in ruby on rails? 回答1: A scope is a subset of a collection. Sounds complicated? It isn't. Imagine this: You have Users

Access named scope dynamically

ε祈祈猫儿з 提交于 2019-12-13 03:06:27
问题 If I have 3 named scopes like class Foo scope :test1, ... scope :test2, ... scope :test3, ... And a function def x(variable) end where variable is a string("test1","test2" or "test3") How can I access the named scope just by knowing that variable's value ? Something like Foo.variable 回答1: You would call Foo.public_send(variable) . 来源: https://stackoverflow.com/questions/15000854/access-named-scope-dynamically

what is the difference between named_scope and method?

蓝咒 提交于 2019-12-12 01:49:06
问题 named_scope or scope how difference with class method. named_scope :active, :conditions => {:status => 'Active'} def self.active self.find(:all, :conditions => {:status => 'Active'} end Whats the difference between the two? 回答1: In the end 'scope' will define a chainable class method on your model. That's why every class method, that returns a 'scope' (which is an object of the class ActiveRecord::Relation) can be used in the same way a definied scope / named_scope can. If you want to find

has_many and sum named_scope

与世无争的帅哥 提交于 2019-12-11 18:33:43
问题 I have this situation: Stories has many Tasks Tasks have an integer called hours_left I need a named scope to find Stories which all its tasks has more than 0 hours left . Based on this post. I wrote this: class Story has_many :tasks named_scope :uncompleted, { :joins=>["INNER JOIN tasks ON tasks.story_id = stories.id"], :group=> 'stories.id', :select=>'stories.*, SUM(tasks.hours_left) AS sum_amount', :having=>"sum_amount > 0" } end But Story.uncompleted returns an empty array. Can you help

Named Scope to only search first results of joined table

此生再无相见时 提交于 2019-12-11 06:31:15
问题 I'm having trouble with a named scope, SQL not my strong suit. I would like to return ALL Machines which had it's LAST test fail. My Machines model: has_many :lodged_tests, :dependent => :destroy has_one :last_test, :class_name => 'LodgedTest', :order => 'created_at DESC' named_scope :last_test_failed, :joins => :last_test, :conditions => [ "lodged_tests.is_passed = ?", false] The named_scope does work except it returns Machines which have ANY failed tests. I need it to return machines which

Default conditions for Rails models

青春壹個敷衍的年華 提交于 2019-12-11 03:38:57
问题 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 possible to do that? What I could do now is just using a named scope having :conditions => {:deleted => false} Is there a better way to do it so that When I do Item.other_named_scope , I could find all those not-deleted items? 回答1: You can use default

Rails 3: Correct syntax for named_scope with method call and model associations

风流意气都作罢 提交于 2019-12-11 00:59:20
问题 I have four models in my application, defined as follows class User < ActiveRecord::Base has_many :comments has_many :geographies has_many :communities, through: :geographies class Comment < ActiveRecord::Base belongs_to :user class Community < ActiveRecord::Base has_many :geographies has_many :users class Geography < ActiveRecord::Base belongs_to :user belongs_to :community Users can post comments, which are associated with one or more communities through the geography table. My task is the