草庐IT

convenience-methods

全部标签

ruby-on-rails - ruby /RoR : calling original method via super()?

在RoR应用程序中,我想在我的一个模型中专门化ActiveRecord的update_attributes()方法,提取一些属性进行特殊处理,并将其余属性传递给原始的update_attributes()方法。详情:classPremise对super.update_attributes(attr)的调用引发错误:undefinedmethod`update_attributes'fortrue:TrueClass...这让我怀疑我真的不了解Ruby中的super关键字。我错过了什么?具体来说,如何调用原始的update_attributes()方法? 最佳

ruby-on-rails - 工厂女郎 + RSpec : undefined method 'create' when create(:user)

不能调用“dummy=create(:user)”来创建用户。来回走了好几个小时。/home/parreirat/backend-clone/Passworks/spec/models/user_spec.rb:15:in`block(2levels)in':undefinedmethod`create'for#(NoMethodError)"这是工厂,users.rb:FactoryGirl.definedofactory:userdoemail'heheheheheheh@gmail.com'password'chucknorris'name'luismendes'endend这就

ruby-on-rails - 如何检查是否存在值不为 "undefined local variable or method"的变量?

这是一个常见的模式:如果变量不存在,我会得到一个未定义的局部变量或方法错误。现有代码有ifvariable_name.present?但这并没有说明变量不存在。我如何检查变量的值并说明它根本不存在?我试过:if(defined?mmm)thenifmmm.present?thenputs"true"endend但Ruby仍然检查内部mmm.present?并在它不存在时抛出“nosuchvariable”。我确信对此有一个共同的模式/解决方案。 最佳答案 将present?更改为!=''并使用&&运算符,它仅在第一个表达式为真时才尝

ruby-on-rails - 摩卡 : How to add expectation of a method when there are multiple invocations with different parameters

我有一个RailsControllerAction要测试。在那个Action中,一个方法User.can?使用不同的参数多次调用。在其中一个测试用例中,我想确保User.can?('withdraw')被调用。但我不关心User.can的调用?与其他参数。defaction_to_be_tested...@user.can?('withdraw')...@user.can?('deposit')...end我在测试中尝试了以下:User.any_instance.expects(:can?).with('withdraw').at_least_once.returns(true)但是测

ruby-on-rails - 使用 define_method 动态定义 setter 方法?

我使用大量迭代来定义模型中的便捷方法,例如:PET_NAMES.eachdo|pn|define_method(pn)do......end但我从来没有能够动态定义setter方法,即:defpet_name=(name)...end像这样使用define_method:define_method("pet_name=(name)")do...end有什么想法吗?提前致谢。 最佳答案 这是一个在用于扩展类的模块中使用define_method的完整示例:moduleVerboseSetterdefmake_verbose_sette

ruby-on-rails - 无方法错误 : undefined method `on' for main:Object

当我尝试bundleexeccapproductiondeploy--trace时,我收到一条错误消息:deploy@h2540559:/www/apps/foodsoft$bundleexeccapproductiondeploy--trace**Invokeproduction(first_time)**Executeproduction**Invokeload:defaults(first_time)**Executeload:defaults**Invokervm:hook(first_time)**Executervm:hookcapaborted!NoMethodError

ruby - 什么时候用undef_method,什么时候用remove_method?

我想重新定义一个方法,但要避免与之相关的警告。我应该使用undef_method还是remove_method来这样做?(是的,重新定义方法有点老套。我这样做是因为我有一些内存,我想在单元测试运行时使用,而不是在程序本身运行时使用。) 最佳答案 来自finemanual:undef_method(symbol)→selfPreventsthecurrentclassfromrespondingtocallstothenamedmethod.Contrastthiswithremove_method,whichdeletestheme

ruby-on-rails - 无方法错误 : undefined method `safe_constantize'

我有一个简单的模型:classPost它有一个名为type的INT列当我创建记录时:Post.create(:type=>1)我得到:NoMethodError:undefinedmethod`safe_constantize'for1:Fixnum我做错了什么? 最佳答案 就像这样:在Fixnum上没有名为safe_constantize的方法。type用于SingleTableInheritance.您通常会在应用程序中将已知模型的字符串表示形式作为type值。参见railsguides

ruby-on-rails - 切换到 Rails 4.1 时出现错误 - `method_missing':ActiveRecord::Base:Class (NoMethodError) 的未定义方法 `whitelist_attributes='

从Rails4.0切换到Rails4.1时出现此错误:activerecord-4.1.8/lib/active_record/dynamic_matchers.rb:26:in`method_missing':undefinedmethod`whitelist_attributes='forActiveRecord::Base:Class(NoMethodError)我没有在我的应用程序的任何地方使用attr_accessible或attr_protected所以我想知道为什么我有问题。当迁移到Rails4.0时,我已经安装了我的application.rb:配置/应用程序.rb:c

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q