简体   繁体   中英

Rails NoMethodError: undefined method `compact' error

I am a rails newbie and cant figure out what is wrong with the following relation. 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

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

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.

You should do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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