3

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?

1
  • I have used a class name with that, would that be a problem ? I tried changing that to questions but the error is the same
    – pariwesh
    Jan 6, 2015 at 4:51

1 Answer 1

0

Because Rep has_many questions, Rep.questions is an array.

You should do the following:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.