Tag: 命名范围

undefined方法`default_scoped?’ 在访问范围时

我在访问范围时遇到此错误。 这是AR模型 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 当我尝试使用StatisticVariable.logins或其他任何范围时,它会给出: NoMethodError: undefined method `default_scoped?’ 如果我将范围配置为类方法,那么它可以完美地工作。 def self.registrations where(code: ‘registrations’).first end 请指导我理解并解决这个问题。

Rails:可以在模块中定义命名范围吗?

假设有3个模型:A,B和C.这些模型中的每一个都具有x属性。 是否可以在模块中定义命名范围并将此模块包含在A,B和C中? 我试图这样做,并收到一条错误消息,说明scope未被识别…

named_scope和方法有什么区别?

named_scope或scope与class方法有何不同。 named_scope :active, :conditions => {:status => ‘Active’} def self.active self.find(:all, :conditions => {:status => ‘Active’} end 这两者有什么区别?

named_scope的问题会导致will_paginate出错 – 如何在计数中包含group_by?

[rails 2.3.12] named_scope: named_scope :order_by_price, lambda {{:joins => :variants, :group => “products.id”, :order => “MAX(price)”}} 安慰: 1. > Product.order_by_price.size => 21 2. > p = Product.order_by_price > p.size => 4 SQL查询: 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 […]

带有has_scope的Rails 4:无声地失败

我有一个Rails 4.0.0应用程序,并希望包含has_scope gem。 非常简单和直接的资源: 模型: class Thing client { where(:client => client)} scope :by_name, -> name { where(:name => name)} 控制器: class ThingsController [:index, :edit] has_scope :by_client, :type => :integer has_scope :by_name …. def index @things = apply_scopes(Thing).all puts current_scopes end current_scopes始终是一个空哈希,无论应用什么范围,Thing.all都会传递给视图。 范围本身是正确的,并正确返回过滤后的记录参数是有的:例如: Started GET “/things?client=113” for 127.0.0.1 at 2014-07-26 16:07:32 +0200 Processing by ThingsController#index […]

如何调用所有评论的post(在这种情况下为锻炼),其中exercise.user_id == current_user.id?

我创建了一个Ruby on Rails应用程序,用户可以在其中记录他们的锻炼,其他用户可以对这些锻炼进行评论。 我正在使用仪表板资源来聚合current_user的信息。 我试图在current_user的训练中显示最近的评论,但似乎无法弄清楚如何正确地做到这一点。 我想我需要一个我还不够的named_scope。 我基本上希望应用程序循环遍历评论表,但只返回对锻炼的评论,其中workout.user_id == to current_user.id。 /views/dashboard/index.html.erb dashboard_controller.rb def index @comments = Comment.all(:order => “created_at DESC”, :limit => 10) @workouts = Workout.all(:order => “created_at DESC”, :limit => 10) end *我认为我不需要@workouts系列,但无论如何都要。

Rails测试具有日期范围的named_scope

情景 我在名为’last_week’的模型上有一个named_scope。 它所做的就是从上周获取记录。 我想测试这个,我的方法是测试返回的结果是否在一定范围内。 我不知道这是否是在类方法上测试此function的正确方法。 我不能使用RSpec,Shoulda或其他第三方客户端插件。 如果我愿意,我只允许使用摩卡。 # Model class Article { :created_at => 1.week.ago..DateTime.now.end_of_day } end #Test class ArticleTest < ActiveSupport::TestCase test "named scope :last_week " do last_week_range = DateTime.now.end_of_day.to_i – 1.week.ago.to_i assert_in_delta last_week_range, Article.last_week.first.created_at – Article.last_week.last.created_at, last_week_range end end 寻找关于这种方法的正确与错误的反馈。

是否有可能否定Rails 3中的范围?

我的类名为Collection我有以下范围: scope :with_missing_coins, joins(:coins).where(“coins.is_missing = ?”, true) 我可以运行Collection.with_missing_coins.count并得到一个结果 – 它很棒! 目前,如果我想获得不丢失硬币的collections品,我会添加另一个范围: scope :without_missing_coins, joins(:coins).where(“coins.is_missing = ?”, false) 我发现自己写了很多这些“相反”的范围。 是否有可能在不牺牲可读性或求助于lambda /方法(将true或false作为参数)的情况下获得范围的反面? 像这样的东西: Collection.!with_missing_coins

ruby on rails命名范围实现

来自Agile Web Development With Rails一书 class Order [‘updated {:pay_type => :check} end 该声明 orders = Orders.checks.last_n_days(7) 将导致只对数据库进行一次查询。 rails如何实现这一目标? 我是Ruby的新手,我想知道是否有一个允许这种情况发生的特殊结构。 为了能够链接这样的方法,named_scope生成的函数必须返回自己或者可以进一步限定范围的对象。 但Ruby如何知道它是最后一个函数调用,它现在应该查询数据库? 我问这个是因为上面的语句实际上是查询数据库而不只是返回一个由链接产生的SQL语句。

带有连接的Rails named_scopes

我正在尝试创建一个使用连接的named_scope,但是虽然生成的SQL看起来正确,但结果却是垃圾。 例如: class Clip “INNER JOIN series ON series.id = clips.owner_id INNER JOIN shows on shows.id = series.show_id”, :conditions=>”shows.visible = 1 AND clips.owner_type = ‘Series’ ” } (一个Clip由一个系列拥有,一个系列属于一个Show,一个Show可以是可见的或不可见的)。 Clip.all: SELECT * FROM `clips` Clip.visible.all: SELECT * FROM `clips` INNER JOIN series ON series.id = clips.owner_id INNER JOIN shows on shows.id = series.show_id WHERE (shows.visible = […]