Reputation: 24886
I have a Mongoid model that looks like this:
class V1Cache
include Mongoid::Document
field :boro, :type => String
embeds_one :cache_data
end
class CacheData
include Mongoid::Document
field :tax_block, :type => String
embedded_in :v1_cache, :inverse_of=>:cache_data
end
I was thinking of writing a create statement that looks like this:
V1Cache.create(:boro=>"boro",:cache_data=>{:tax_block=>"sadsd"})
I tried this statement and it failed with this error: uninitialized constant CacheDatum.
That made me wonder, how can I create an embedded model simultaneously as I create the parent model?
Upvotes: 2
Views: 445
Reputation: 15736
I think whats happening here is that mongoid is trying to infer the name of the embedded class from the name of the :cache_data association. It uses the ActiveSupport::Inflector to convert 'cache_data' from a plural noun to a singular class name and gets 'CacheDatum' because it considers 'datum' to be the singular for 'data'.
In cases like this where the inflector isn't giving you the right answer you just need to give a hint. In this case I think you can explicitly specify the class name (line 4):
embeds_one :cache_data, :class_name => 'CacheData'
Otherwise the syntax is fine.
Upvotes: 7