ActiveRecord and Mogoid :: Document: Associations

I have one model based on ActiveRecord and another based on Mogoid :: Document. Is co-merging possible?

For example, two models:

class User < ActiveRecord::Base has_one :avatar, :dependent => :destroy end class Avatar include Mongoid::Document field :file_name end 

And load the user avatar as follows:

 @user.avatar.file_name 

Thanks!

+4
source share
4 answers

You cannot use ActiveRecord relationships.

You can still bind two objects using instance methods, for example:

 class User < ActiveRecord::Base def avatar Avatar.where(:user_id => self.id).first end def avatar=(avatar) avatar.update_attributes(:user_id => self.id) end end 

It would be interesting to encapsulate this in a module:) ...

+10
source

Maybe with the Tenacity Jewel: https://github.com/jwood/tenacity

We have been using it in production for several months, and it works very well.

+3
source

Indeed, after the same decision. I wrote https://rubygems.org/gems/mongo_mysql_relations to simplify it, but basically this is the solution proposed above, but less manual.

Github is at https://github.com/eladmeidar/MongoMysqlRelations

0
source

No, It is Immpossible. ActiveRecord expects the association to be associated with an AR object. Previously, you could associate Mongoid with AR, but now it does not work.

-2
source

All Articles