Access belongs_to on a Mongoid::Document subclass

25 views
Skip to first unread message

Myth17

unread,
May 7, 2013, 5:08:36 AM5/7/13
to rubyonra...@googlegroups.com

I have a model 'Index' as:

class Index
  include Mongoid::Document

  belongs_to :project  
end

Another model PercentileRankIndex inherits Index

class PercentileRankIndex < Index

     def self.model_name
         Index.model_name
     end

end

Suppose I do :

 p = Index.first (OR EVEN) p = PercentileRankIndex.first

I get this :

#<PercentileRankIndex _id: 51630ece34b2613d27000011, project_id: "51630ece34b2613d27000010", enabled: true, _type: "PercentileRankIndex", :enabled: "true"> 

However on doing

p.project

=>nil

The belongs_to relationship isnt working on child class. Why? How can I fix it?


dbkbali

unread,
May 8, 2013, 12:30:54 PM5/8/13
to rubyonra...@googlegroups.com
Just a hunch, but for starters index is a reserved mongoid word so for starters try changing your class name.

Regards,

David

dbkbali

unread,
May 8, 2013, 12:31:15 PM5/8/13
to rubyonra...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages