Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid relations with devise

I have some rails application with MongoDB, Mongoid mapper and devise. Аuthorized user can create, edit, delete post(scaffold) and comment this posts. I take for comment model example of Ryan Bates screencasts, 238 episode "Mongoid".

comment.rb

class Comment
  include Mongoid::Document
  field :name
  field :content
  embedded_in :post, :inverse_of => :comments
end

post.rb

class Post
      include Mongoid::Document
      field :name
      field :content
      validates_presence_of :name
      embeds_many :comments
    end

user.rb

class User
  include Mongoid::Document
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable

  field :username

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :comments
  references_many :post

end

But when I try to register new user, in registration form push "Sign up", I see this error

Mongoid::Errors::MixedRelations in Devise::RegistrationsController#create

Referencing a(n) Comment document from the User document via a relational association is not allowed since the Comment is embedded.

I start this apllication with Mysql db, and then decided to move into mongo. Where is my mistake?

like image 424
Eugene Avatar asked Jun 05 '26 14:06

Eugene


1 Answers

Since Comment is embedded in Post you should have User referencing Post. Try removing has_many :comments in User.

like image 103
jcollum Avatar answered Jun 07 '26 12:06

jcollum