Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoidalize throws NameError, uninitialized constant Mongoid::Relations #55

Open
Eidar opened this issue Sep 18, 2018 · 9 comments
Open

Comments

@Eidar
Copy link

Eidar commented Sep 18, 2018

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 :)

@Eidar Eidar closed this as completed Sep 18, 2018
@Eidar Eidar reopened this Sep 18, 2018
@Eidar
Copy link
Author

Eidar commented Sep 18, 2018

Mistakenly closed the issue, no worries.

@joshed-io
Copy link
Owner

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?

@Eidar
Copy link
Author

Eidar commented Sep 20, 2018

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

@joshed-io
Copy link
Owner

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.

@Eidar
Copy link
Author

Eidar commented Oct 1, 2018

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

@joe1chen
Copy link
Contributor

joe1chen commented Oct 8, 2018

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.

@joe1chen
Copy link
Contributor

joe1chen commented Oct 8, 2018

No need for a test repository. Just updating the gems to Mongoid 7 and running the existing specs is enough to reproduce the issue.

@joe1chen
Copy link
Contributor

joe1chen commented Oct 8, 2018

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.

@joshed-io
Copy link
Owner

Thanks @joe1chen for looking at that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants