草庐IT

ruby - 续集中的 default_scope

在ActiveRecord中有一个default_scope类方法来指定默认范围。例如classUserfalse)endUser.all#=>SELECT*FROMusersWHEREdeleted=0;我如何在Sequel::Model中执行此操作?编辑:经过一番谷歌搜索,我终于找到了一些有用的信息。classUser生成的查询如下所示:User.all#=>SELECT*FROM`users`WHERE(`deleted`ISFALSE)顺便说一下:unscoped等同于unfiltered:User.unfiltered.all#=>SELECT*FROM`users`但是,有

ruby-on-rails - Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' 错误

我正在使用Rails3.2和ActiveAdmin0.4.4开发应用程序。我有一个名为Teaser的模型(/app/models/teaser.rb):classTeasertruemount_uploader:img,TeaserUploaderend然后我向其中添加了ActiveAdmin(/app/admin/teaser.rb):#encoding:UTF-8ActiveAdmin.registerTeaserdoformdo|f|f.inputs"Teaser"dof.input:name,:label=>'Текст'f.input:url,:label=>'Ссылка'

ruby - RSpec 2之前(:suite) variable scope

基本上我想创建一个数组,然后在我的规范中附加到它,然后再最终处理并将它显示给用户。我可以想出一些解决方法,但理想情况下我想执行以下操作。RSpec.configuredo|config|config.before(:suite){@array_of_stuff||=[]}config.after(:suite){process_and_print(@array_of_stuff)}enddefprocess_and_print(array)#dostuffend不幸但并不令人惊讶的是@array_of_stuff不在范围内并且不能从我的规范中追加,这与在before(:all)bloc

ruby-on-rails - 为什么我得到 "including Capybara::DSL in the global scope is not recommended!"

每次我运行规范,即使规范通过,例如$rspecspec/integration/view_homepage_spec.rbincludingCapybara::DSLintheglobalscopeisnotrecommended!.Finishedin0.6174seconds1example,0failuresRandomizedwithseed14130$我的Gemfile有:group:test,:developmentdogem'rspec-rails'gem'capybara'end我的spec_helper有:ENV["RAILS_ENV"]||='test'requir

Ruby 范围,常量优先级 : lexical scope or inheritance tree

我将向您展示来自rubykoans的代码片段教程。考虑下一个代码:classMyAnimalsLEGS=2classBird实际上问题在评论中(我用星号突出显示了它(尽管它打算以粗体显示))。有人可以解释一下吗?提前致谢! 最佳答案 这里有答案:Ruby:explicitscopingonaclassdefinition.但也许它不是很清楚。如果您阅读链接的文章,它将帮助您找到答案。基本上,Bird是在MyAnimals的范围内声明的,在解析常量时具有更高的优先级。Oyster位于MyAnimals命名空间中,但未在该范围内声明。将

ruby-on-rails - 攻击 ActiveRecord : add global named scope

我正在尝试为像这样的ActiveRecord模型提供一组非常通用的命名范围:moduleScopesdefself.included(base)base.class_evaldonamed_scope:not_older_than,lambda{|interval|{:conditions=>["#{table_name}.created_at>=?",interval.ago]}endendendActiveRecord::Base.send(:include,Scopes)classUser如果命名范围应该是通用的,我们需要指定*table_name*以防止命名问题,如果它们是来自

ruby - Rails3 : combine scope with OR

我需要将名称范围与or运算符组合...像这样的东西:classProduct谢谢 最佳答案 来自AreldocumentationTheORoperatorisnotyetsupported.Itwillworklikethis:users.where(users[:name].eq('bob').or(users[:age].lt(25)))ThisRailsCast向您展示如何使用.or运算符。但是,当您拥有ActiveRecord::Relation实例时,它可以与Arel对象一起使用。您可以使用Product.name_a.

ruby-on-rails - 使用 named_scope 获取行数

Rails专家:感谢另一位SO用户,我刚刚发现了named_scope。:)我想获取一组行的计数-即SELECTCOUNT(*)。此外,我希望仍然能够在调用中链接命名范围。这是命名范围的合法(尽管很奇怪)用法吗?named_scope:count,:select=>"COUNT(*)ascount_all"然后我可以做(例如):@foobar=Foobar.count.scope.scope.scope计数通过@foobar.first.count_all访问。(EditedtoaddressAllan'scomments)你可以这样做:@foobar=Foobar.scope.sco

ruby-on-rails - 如何让 Rails 中的 named_scope 返回一个值而不是数组?

我想写一个namedscope从它的id中获取记录。例如,我有一个名为Event的模型,我想使用named_scope来模拟Event.find(id)future的灵active。我在我的模型中使用了这段代码:named_scope:from_id,lambda{|id|{:conditions=>['id=?',id]}}我从我的Controller调用它,如Event.from_id(id)。但我的问题是它返回一组Event对象而不是一个对象。因此如果我想获取事件名称,我必须写event=Event.from_id(id)event[0].name而我想要的是event=Even

ruby-on-rails - rails 4 : how to use named scope with has_many associations

在我的Rails4应用程序项目(模型)has_many视频(模型)中。我在视频模型中有一个命名范围:scope:live,where(is_deleted:0,sent_to_api:1)在我的一个项目View中,我这样做(项目是项目的一个实例):project.videos.live.size我希望得到的是那个特定项目中的项目数量,但我得到的是任何项目中的视频数量。就好像.live不是从.videos返回一个子集而是替换它。我看到它解释了here将命名范围相互链接起来应该与逻辑AND相结合,但是当应用于“关联方法”时[.videos在这种情况下的正确术语]似乎并不即将发生。正确的做法