Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid关系抛出命名错误:未定义方法';推动';零级:零级_Ruby On Rails_Ruby_Mongoid - Fatal编程技术网

Ruby on rails Mongoid关系抛出命名错误:未定义方法';推动';零级:零级

Ruby on rails Mongoid关系抛出命名错误:未定义方法';推动';零级:零级,ruby-on-rails,ruby,mongoid,Ruby On Rails,Ruby,Mongoid,编辑:此问题已解决。该问题是由特定于我的应用程序的冲突引起的。另一个模块动态创建了一个名为.sources的方法。我能够通过删除关系和检查对象方法列表来进行故障排除。谢谢你 我在Mongoid 4.0中使用Rails 4.1,并设置了如下关系: class Organization include Mongoid::Document has_many :sources end 及 在rspec测试中,我有: require 'rails_helper' RSpec.describe P

编辑:此问题已解决。该问题是由特定于我的应用程序的冲突引起的。另一个模块动态创建了一个名为
.sources
的方法。我能够通过删除关系和检查对象方法列表来进行故障排除。谢谢你

我在Mongoid 4.0中使用Rails 4.1,并设置了如下关系:

class Organization
  include Mongoid::Document
  has_many :sources
end

在rspec测试中,我有:

require 'rails_helper'
RSpec.describe PartnershipsController, :type => :controller do
describe "POST #record" do
  it "should create a partnership for the source's organization" do
    organization = FactoryGirl.create(:organization)
    source = FactoryGirl.create(:source)
    organization.sources.push source
  end
end
后面还有更多内容,但在
organization.sources.push source
行中测试失败,原因是:

undefined method `push' for nil:NilClass
我不明白为什么会出错。看起来我应该这样添加相关的源代码,但到目前为止还没有骰子。建立这种关系的正确方法是什么

编辑:添加工厂

这是组织工厂,以防它有助于澄清某些问题:

FactoryGirl.define do
  factory :organization do
    app_name = Faker::App.name
    company_name = Faker::Company.name
    sequence(:name) { |n| "#{([app_name, company_name].sample)}#{n}" }
    defaults_hash = { 'item_type' => 'charity', 'child_item_type' => 'product'}
    defaults defaults_hash
  end
end

显示organization Factory将在rails Concoble中返回表达式
organization.new.sources
?这个
Organization.last.sources
使用mongoid 5和factory girl 4.5的新应用程序运行良好。您确定您的组织正在保存某些模型级验证并失败吗?谢谢@Anthony,但在更新mongoid时遇到了问题。从bundle中获取此错误:
mongoid rspec(>=0)ruby依赖于mongoid(~>4.0.0)ruby
@chumakoff
Organization.new.sources=>nil
&
Organization.last.sources=>{$in'=>[BSON::ObjectId('54eb8b48746d6fbaae67000')]}选项:{}class:Source embedded:false>
显示organization Factory在rails Concoble中返回表达式
organization.new.sources
?这个
Organization.last.sources
使用mongoid 5和factory girl 4.5的新应用程序运行良好。您确定您的组织正在保存某些模型级验证并失败吗?谢谢@Anthony,但在更新mongoid时遇到了问题。从bundle中获取此错误:
mongoid rspec(>=0)ruby依赖于mongoid(~>4.0.0)ruby
@chumakoff
Organization.new.sources=>nil
&
Organization.last.sources=>{$in=>[BSON::ObjectId('54eb8b48746d6fbaae67000')]}选项:{}类:Source embedded:false>
FactoryGirl.define do
  factory :organization do
    app_name = Faker::App.name
    company_name = Faker::Company.name
    sequence(:name) { |n| "#{([app_name, company_name].sample)}#{n}" }
    defaults_hash = { 'item_type' => 'charity', 'child_item_type' => 'product'}
    defaults defaults_hash
  end
end