简体   繁体   English

Rails NoMethodError:未定义的方法“紧凑”错误

[英]Rails NoMethodError: undefined method `compact' error

I am a rails newbie and cant figure out what is wrong with the following relation. 我是Rails新手,无法确定以下关系有什么问题。 I have some models as below class Question include Mongoid::Document has_one :dep_q, class_name: 'DepQ' belongs_to :rep, class_name: "Rep" end 我有一些以下类的模型:类问题包括Mongoid :: Document has_one:dep_q,class_name:'DepQ'Emirates_to:rep,class_name:“ Rep”结尾

class DepQ
  include Mongoid::Document
  has_one :data_holder, class_name: 'DataHolder'
  belongs_to :question, class_name: 'Question'
end

class DataHolder
  include Mongoid::Document
  has_one :rep, class_name:"Rep"
  belongs_to :dep_q, class_name:"DepQ"
end

class Rep
  include Mongoid::Document
  has_many :question, class_name: 'Question'
  belongs_to :data_holder, class_name: 'DataHolder'
end

I don't know what wrong I am doing here. 我不知道我在这里做什么错。 I don't know why i am getting this error. 我不知道为什么我会收到此错误。 I can do the following 我可以做到以下几点

a = Question.new
a.dep_q = DepQ.new
a.dep_q.data_holder = DataHolder.new
a.dep_q.data_holder.rep = Rep.new

But, once I create a new Question under Rep I get the following error 但是,一旦我在Rep下创建了一个新的Question,我就会收到以下错误

NoMethodError: undefined method `compact' for #<Question:0x00000005168cf8>

Why am I seeing this error and how can this be solved? 为什么会看到此错误,如何解决?

Because Rep has_many questions , Rep.questions is an array. 由于Rep has_many questions ,因此Rep.questions是一个数组。

You should do the following: 您应该执行以下操作:

r = Rep.new
q = Question.new
r.questions << q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM