草庐IT

module_name

全部标签

ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

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

【笔记】internal/modules/cjs/loader.js:985 throw err; ^ Error: Cannot find module ‘node:util‘

[root@localhostusr]#cnpm-vinternal/modules/cjs/loader.js:985throwerr;^Error:Cannotfindmodule‘node:util’Requirestack:/usr/local/node/lib/node_modules/cnpm/bin/cnpmatFunction.Module._resolveFilename(internal/modules/cjs/loader.js:982:15)atFunction.Module._load(internal/modules/cjs/loader.js:864:27)atM

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在这种情况下的正确术语]似乎并不即将发生。正确的做法

ruby-on-rails - railstutorial.org 中的 SessionsHelper : Should helpers be general-purpose modules for code not needed in views?

railstutorial.org有一个让我觉得有点奇怪的建议。Itsuggeststhiscode:classApplicationControllerincludeSessionsHelper使方法在ApplicationController中可用,是的,但它也使它们在任何View中都可用。我知道身份验证/授权是交叉的,但这真的是最好的地方吗?在我看来,这可能范围太广了。将实现有条件重定向(如railstutorial.org示例所做的)的before_filter的代码放在更通常包含View助手的模块中似乎令人惊讶。将View中不需要的功能放在ApplicationControl

ruby-on-rails - 带连接的 Rails named_scopes

我正在尝试创建一个使用连接的named_scope,但尽管生成的SQL看起来正确,但结果却是垃圾。例如:classClip"INNERJOINseriesONseries.id=clips.owner_idINNERJOINshowsonshows.id=series.show_id",:conditions=>"shows.visible=1ANDclips.owner_type='Series'"}(一个剪辑属于一个系列,一个系列属于一个节目,一个节目可以是可见的或不可见的)。Clip.all可以:SELECT*FROM`clips`Clip.visible.all可以:SELEC

ruby-on-rails - Rails belongs_to 关联(与 :class_name) returns nil

我是Rails开发的新手,遇到了一个小的关联问题。我想给一个关联起一个不同于它链接到的模型的名字。我有以下两个模型:classUser"User"#Sowecancallevent.admintoretrievetheUserwhoownsthisEventend我按如下方式构建用户:event=event.create!:title=>"NewEvent"user=User.create!:username=>"thinkswan"user.events当我进入控制台时,我收到以下信息:irb>user=User.find(1)irb>user.events=>[#]irb>even

ruby-on-rails - Rails 4 迁移 : has_and_belongs_to_many table name

我目前正在尝试将Rails3.2应用程序切换到Rails4.0。但是我对has_and_belongs_many模型有一个问题。我创建了一个测试应用程序,但我遇到了同样的问题。这就是我所做的:创建了两个模型:foo_clip和foo_urlclassFooClip在此之后我更新了迁移文件:classCreateFooClips现在我已经为has_and_belongs_to_many表创建了迁移文件classCreateFooClipsFooUrls作为最后一步,我创建了一个用于测试的种子文件:foourl1=FooUrl.create!(:url=>'http://www.googl

ruby - Ruby 中的 "include module"和 "extend module"有什么区别?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:WhatisthedifferencebetweenincludeandextendinRuby?给定:modulemy_moduledeffoo...endend问题一有什么区别:classAincludemy_moduleend和classAextendmy_moduleend问题二将foo视为实例方法还是类方法?换句话说,这是否等同于:classAdeffoo...endend或:classAdefself.foo...endend?

ruby-on-rails - ruby /rails : How to determine if module is included?

在这里扩展我的问题(ruby/rails:extendingorincludingothermodules),使用我现有的解决方案,确定我的模块是否包含在内的最佳方法是什么?我现在所做的是在每个模块上定义实例方法,这样当它们被包含时,一个方法就可用,然后我只是向父模块添加一个捕获器(method_missing())所以如果它们不包括在内,我可以catch。我的解决方案代码如下:moduleFeaturesFEATURES=[Running,Walking]#includeFeatures::RunningFEATURES.eachdo|feature|includefeatureen