mongoid_alize
mongoid_alize copied to clipboard
Mongoidalize throws NameError, uninitialized constant Mongoid::Relations
project isn't complicated, but finding my way around mongoid relationships has been. It would be nice to find some in depth explanation.
I couldn't make my code work however I set them up so I managed to find Mongoid_Alize: https://github.com/dzello/mongoid_alize
Yet when I do it with alize or alize_to/_from, console gives me back an error on Mongoid::Relations in Ruby2.3.3/lib/ruby/gems/2.3.0/bundler/gems/mongoid_alize-b9175f4e3165/lib/mongoid/alize/macros.rb:79:in `_alize_relation_types'"
I have also included eager loading with identity_map_enabled: true in mongoid.yml
So here's the code:
Currency.rb
class Currency
include Mongoid::Document
include Mongoid::Alize
field :name, type: String
field :state, type: String
field :code, type: Integer
field :unit, type: Integer
has_many :currency_value
has_many :value, :class_name => 'Value', :inverse_of => :currency
alize_to :value
end
Value.rb
class Value
include Mongoid::Document
include Mongoid::Alize
field :date, type: DateTime
field :buying, type: String
field :middle, type: String
field :selling, type: String
belongs_to :value_currency, :class => 'Currency', :inverse_of => :currency_value
belongs_to :currency, :class_name => 'Currency', :inverse_of => :value
alize_from :currency
end
ApiService.rb
class ApiService
# Has some api data coming from external function getApiData
def update()
@arrayOfHashes= getApiData() #array of hashes
@arrayOfHashes.each do |hash|
@currency = Currency.new({
name: hash["Name"],
state: hash["State"],
code: hash["Currency code"],
unit: hash["Unit"]
})
@currency.create_value(
date => hash["Date of value"],
buying => hash["Value when buying"],
middle => hash["Middle value"],
selling => hash["Value when selling"])
@currency.save
end
return @currency
end
end
Expected result: save all 13 iterations in MongoDB with their relations.
It would be much obliged to hear some explanation here cause I'm uninspired and stuck in place. I have some background in programming and I have a feeling that it shouldn't be that complicated.
I can save either Currency model or Value but not both with their relations. Other idea is to do it manually by adding a custom foreign_id and making a method in the model which will populate hash. But that could lead to future performance issues.
Any help is appreciated, thank you :)
Mistakenly closed the issue, no worries.
Hi @Eidar, I'm wondering if there isn't a mismatch between the versions of mongoid_alize and mongoid. Can you create a fresh repo with just the Gemfile, your models, and enough code to reproduce the error you're seeing?
Yes, same problem still.
Ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32] Rails 5.1.6 MongoDB: v4.0.2 Mongoid 7.0.2 mongoid_alize: 0.6.0
Are you able to share that fresh repo with me? I want to help but have very limited time this week.
One suggestion would be to try with Mongoid up to 6.4. The last contribution (from @joe1chen) stated that as the most recent supported version.
Here you go:
https://github.com/Eidar/mongoid_alize_test
You can take it easy, I solved it in some other way but I'm still interested in a solution here. As you say, you have very limited time and I'm not in that much of a hurry :D
I also saw the issue with Mongoid 7 and did not have the time to look into the issue with Mongoid 7. Since we are not using Mongoid 7, there was not a big reason for us to investigate this any further. But yes we should figure out the issue with Mongoid 7 eventually.
No need for a test repository. Just updating the gems to Mongoid 7 and running the existing specs is enough to reproduce the issue.
Ok, it looks like this issue with Mongoid 7 was also raised in the mongoid-rspec library and subsequently fixed (see https://github.com/mongoid/mongoid-rspec/pull/212/files). Looks like Mongoid::Relations needs to be replaced with Mongoid:Associations in version 7 and up. The pull request from the mongoid-rspec has all the details. If someone has some time to replicate this work, please do so.
Thanks @joe1chen for looking at that!