草庐IT

2Classes1Method

全部标签

ruby-on-rails - 为什么 alias_method 在 Rails 模型中失败

classCountry在第一次调用alias_method时失败:NameError:undefinedmethod`langEN'forclass`Country'我的意思是当我执行Country.first时它失败了。但在控制台中,我可以成功调用Country.first.langEN,并且看到第二个调用也有效。我错过了什么? 最佳答案 ActiveRecord使用method_missing(AFAIK通过ActiveModel::AttributeMethods#method_missing)在第一次调用时创建属性访问器和

ruby-on-rails - 名称错误 : undefined local variable or method `logger'

当我运行“脚本/服务器”时,一切正常,但是当我运行我的单元测试(raketest:units)时,我得到了下面的错误,我不知道如何解决这个问题.错误NameError:undefinedlocalvariableormethod`logger'for#/Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in`method_missing'/Users/kamilski81/Sites/pe/vitality_mall/lib/

ruby - RSpec NoMethodError : "undefined method ` describe' for main Object"

我正在尝试学习Rspec。我在eclipse中的ruby项目如下-代码-require'rspec'require'./RubyOffRailsTuts/classes/furlong'describeFurlongdoend错误-/RubyOffRailsTuts/specs/furlong_spec.rb:6:in`':undefinedmethod`describe'formain:Object(NoMethodError)没有在网上得到任何有用的答案。我该如何解决这个问题? 最佳答案 作为RSpec.describe的前缀d

ruby - 如何在 initialize() 中使用 define_method

尝试在initialize中使用define_method但得到undefined_methoddefine_method。我做错了什么?classCdefinitialize(n)define_method("#{n}"){puts"somemethod#{n}"}endendC.new("abc")#=>NoMethodError:undefinedmethod`define_method'for# 最佳答案 我怀疑您正在寻找define_singleton_method:define_singleton_method(symb

ruby - 在 Ruby 的 define_method 中使用 yield

是否可以让yield关键字在给定define_method的block内工作?简单示例:classTestdefine_method:testdo|&b|putsb#=>#yieldendendTest.new.test{puts"Hi!"}此代码在Ruby1.8.7和1.9.0中都会产生以下错误:test.rb:4:in`test':noblockgiven(LocalJumpError)fromtest.rb:8奇怪的是bblock变量!=nil但block_given?返回false。不识别Proc对象的block是Ruby的故意行为吗?编辑:向Beerlington的回答致意:

ruby - 轨道 3/ ruby : ActiveRecord Find method IN condition Array to Parameters single quote issue

我在微博模型上创建了一个方法,它接受一个user_id数组。在此方法中,我使用以下“查找”方法来提取数组中所有用户的所有帖子。find(:all,:conditions=>["user_idIN(?)",args.join(',')])但是当ActiveRecord为这个查询生成SQL时,它用单引号将逗号分隔的ID列表括起来。这会导致查询只提取单引号内第一个数字的帖子,而不是所有数字。SELECT`microposts`.*FROM`microposts`WHERE(user_idIN('3,4,5'))ORDERBYmicroposts.created_atDESC查询应该看起来像这

ruby - 如何使用 define_method 指定方法默认参数?

define_method可用于定义方法:define_method(:m)do|a|end等同于:defm(a)end但是,以下使用define_method的等效形式是什么:defm(a=false)end请注意,我需要能够在不提供任何参数的情况下调用m()。 最佳答案 这实际上就像您在Ruby1.9中所期望的那样工作!define_method:mdo|a=false|end如果您需要1.8兼容性,但不一定需要闭包来定义您的方法,请考虑使用带有字符串参数的class_eval并定期调用def:class_eval否则请按照ph

ruby-on-rails - Carrierwave,MiniMagick - NoMethodError : undefined method `size' for nil:NilClass

在暂存过程中,我在通过carrierwave和minimagick上传和调整图像大小时遇到​​以下错误。在本地一切正常。载波(0.9.0)迷你魔法(3.7.0)irb(main):003:0>PicturePost.create(remote_content_url:'http://www.imagpress.com/img/slider/slider_1.jpg')NoMethodError:undefinedmethod`size'fornil:NilClassfrom/home/deploy/apps/staging/blog/shared/bundle/ruby/1.9.1/g

ruby-on-rails - rails : Overriding ActiveRecord association method

有没有办法覆盖ActiveRecord关联提供的方法之一?例如,我有以下典型的多态has_many:throughassociation:classStory:taggablehas_many:tags,:through=>:taggings,:order=>:nameendclassTag:destroyhas_many:stories,:through=>:taggings,:source=>:taggable,:source_type=>"Story"end您可能知道,这会向Story模型添加一大堆关联方法,例如标签、标签我如何着手覆盖这些方法之一?特别是tagsdeftags调

ruby-on-rails - rails 3 : alias_method_chain still used?

我刚刚阅读有关Rails3的Gems/Plugin开发并遇到了thispost这表示不再使用alias_method_chain。我可以看到该方法仍然存在于activesupport-3.0.0/lib/active_support/core_ext/module/aliasing.rb中。我还应该在Rails3中使用alias_method_chain吗?是this是否仍然反射(reflect)了Rails3中想要修改ActiveRecord的gem/插件的最佳实践? 最佳答案 不,它已被巧妙地使用模块中的方法覆盖和super关键