I want to create an app that uses both MongoDB and MySQL. Specifically, I want mongodb to store all the users’ comments while MySQL will store the User model.
class User < ActiveRecord::Base
has_many :comments
end
class Comment
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
end
well, everything looks good except when I go to the rails console and run this.
k = Comment.new
k.user = User.first
I got
NoMethodError: User Load (0.3ms) SELECT
users.* FROMusers
WHEREusers._id= 1 Mysql2::Error: Unknown column ‘users._id’ in
‘where clause’: SELECTusers.* FROMusersWHEREusers._id= 1
undefined method `from_map_or_db’ for
It looks like that the := method is looking for the _id of the model instea of the id? Is there a workaround to get this working automatically or do I need to create my own = method?
Has anyone tried the same configuration before? If so, what are the steps to get all these to work?
This is not gonna work like you want it to. Your
belongs_to :userinCommentis telling Mongoid to make this association in MongoDB; in order to make ActiveRecord associations, your class must inherit fromActiveRecord::Baseor includeActiveRecord::Model–and you can’t do both!Probably the best way to do this–and I don’t know how difficult it would be–is to write your own methods to associate the
UsersandCommentstogether.