草庐IT

has_is_baz

全部标签

ruby - 为什么 'undefined method ` assert_equal' ' is thrown even after requiring ' test/unit'

我打开irb并输入:require'test/unit'但是当我使用assert_equal方法时,出现以下错误:NoMethodError:undefinedmethod'assert_equal'formain:Object。为什么即使在需要“测试/单元”之后也会发生这种情况? 最佳答案 assert_equal是在Test::Unit::TestCase的子类上定义的,因此仅在该类中可用。您可能会成功地使用includeTest::Unit::TestCase将这些方法加载到当前范围。更有可能的是,您最好将测试写在一个短文件中

ruby-on-rails - 我如何在 Rails 中测试 belongs_to 和 has_many?

我正在使用rspec并尝试测试我的模型y是否有很多x。我尝试了各种方法,包括遍历方法数组,但似乎无法在网上找到好的方法。那我应该怎么用呢? 最佳答案 无需太多黑客攻击,您就可以使用卓越的gem:http://github.com/carlosbrando/remarkable摘自非凡的文档:describePostdoit{shouldbelong_to(:user)}it{shouldhave_many(:comments)}it{shouldhave_and_belong_to_many(:tags)}end

ruby-on-rails - rake 数据库 :migrate is being aborted due to rake version difference

这个问题在这里已经有了答案:YouhavealreadyactivatedX,butyourGemfilerequiresY(11个答案)关闭8年前。我遇到了错误rakedb:migrate--tracerakeaborted!Youhavealreadyactivatedrake10.1.1,butyourGemfilerequiresrake10.1.0.Usingbundleexecmaysolvethis./Users/iang/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:

ruby-on-rails - Ruby on Rails 在保存之前通过关联对象 has_many

在RubyonRails项目中,我试图在将所有内容保存到数据库之前访问ActiveRecord上的关联对象。classPurchase如果我像这样构建我的对象:purchase=Purchase.new(item_ids:[1,2,3])并尝试保存它item_validation方法还没有填充项目集合,所以即使虽然项目已经设置,但它没有机会调用任何项目的check_something方法。是否可以在我的购买模型和关联模型保留之前访问项目集合,以便我可以对它们运行验证?如果我将item_validation方法更改为:defitem_validationpurchase_items.ea

ruby-on-rails - 通过关联获取 has_many 的 ActiveRecord::RecordInvalid 错误;连接表上的验证问题

我有如下三个关联模型:classProduct当我做类似的事情时:doc=user.documents.builddoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到false和doc上的相应错误。这正是我想要的。但是,如果doc已经存在,例如:doc=user.documents.firstdoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到一个ActiveRecord::RecordInvalid错误。我很清楚为什么会这样——i

ruby-on-rails - rails 引擎 : rake routes show its routes but on rspec execution "No route matches" is thrown

我正在尝试测试我的应用程序正在使用的引擎内部的Controller。规范不在引擎中,而是在应用程序本身中(我试图在引擎中进行测试,但也遇到了问题)。我的引擎有以下routes.rb:Revision::Engine.routes.drawdoresources:steps,only:[]docollection{get:first}endend引擎正常挂载在应用routes.rb上:mountRevision::Engine=>"revision"当我运行rakeroutes时,在最后一行我得到:RoutesforRevision::Engine:first_stepsGET/step

ruby-on-rails - Ruby BigDecimal 圆 : Is this an error?

在用一个表示为BigDecimal的值编写测试时,我遇到了一些奇怪的事情并决定深入研究它。简而言之,四舍五入到小数点后两位的“0.00009”返回为0.01而不是0.00。真的。这是我的脚本/控制台捕获:>>bp=BigDecimal('0.09')=>#>>bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f=>0.09>>bp=BigDecimal('0.009')=>#>>bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f=>0.01>>bp=BigDecimal('0.0009')=>#>>bp.roun

ruby-on-rails - 设置一个 :has_many :through association on a belongs_to association Ruby on Rails

我有三个模型,每个模型都有以下关联:classModel1:model1#willthiswork?isthereanywayaroundthis?endclassModel3:model1#willthiswork?isthereanywayaroundthis?end正如您在评论文本中看到的那样,我已经提到了我需要的内容。 最佳答案 您只需创建访问它的方法classModel2或者,您可以将model3s方法委托(delegate)给model1classModel2:model1end

ruby - Rails 3 Order By Count on has_many :through

我有一个应用程序,我可以在其中列出项目并为每个项目添加标签。模型Items和Tags是这样关联的:classItem:taggingsendclassTagging:taggingsend因此,这种多对多关系允许我为每个Item设置n标签,并且同一个标签可以多次使用。我想列出所有标签,这些标签按与该标签关联的项目数排序。更多使用的标签,首先显示。较少使用,最后。我该怎么做?问候。 最佳答案 Tag.joins(:taggings).select('tags.*,count(tag_id)as"tag_count"').group(:

ruby - "Assignment Branch Condition size for index is too high"是如何工作的?

Rubocop总是报告错误:app/controllers/account_controller.rb:5:3:C:AssignmentBranchConditionsizeforindexistoohigh.[30.95/24]ifparams[:role]@users=@search.result.where(:role=>params[:role])elsifparams[:q]&¶ms[:q][:s].include?('count')@users=@search.result.order(params[:q][:s])else@users=@search.result