0

I'm new to MonogoDB. I've been able to seed data, but not relational stuff.

in my model I have

has_and_belongs_to_many :users

field :encrypted_password, type: String, default: ""

field :name, type: String
field :address, type: String
field :city, type: String
field :state, type: String
field :zip, type: String

In DB's like MySQL, I wouldn't use the above syntax, but instead make a migration file, pushing it to the schema. Here, in Mongo it seems more on the fly.

So with the first line (has_and...) I thought I could seed using

XYZ.create(:name => "x", {...}, user_id=>[1,2])
User.create(:name => "y", {...}, XYZ_id => [1])

This gives me a nomethod error.

Attempted to set a value for 'user_id' which is not allowed on the model XYZ.

So I added the dynamic attribues line:

  include Mongoid::Attributes::Dynamic

On both the models associated models.

When I logged into the user, it didn't know the association (that particular user didn't belong to XYZ.)

Naturally I loaded up Rails C, but did not see nearly what I expected when typing

User.all (or) XYZ.all

I saw instead

2.1.0 :004 > User.all
=> #<Mongoid::Criteria
selector: {}
options:  {}
class:    User
embedded: false>

so I wasn't sure how to debug the issue. How can I be sure to seed the relationship appropriately?

Peege151
  • 1,562
  • 2
  • 21
  • 45

2 Answers2

1

It looks like you are not using any ODM framework for MongoDB. Please note that ActiveRecord does not support MongoDB. Mongoid is a really nice ODM for MongoDB written in Ruby. You can find more information about it here. Here is a Railscast on Mongoid by Ryan Bates. This will help you solve the problem.

That said, as far as seeding data is concerned, go through this and this if you still face problems.

Hope it helps.

Community
  • 1
  • 1
Peeyush
  • 6,144
  • 5
  • 23
  • 37
  • Peeyush, see answer below, perhaps you might have a different answer with the knowledge he is in fact using Mongoid, I'm no expert with Mongoid. – bmeyers Jul 18 '14 at 08:29
1

You are indeed using Mongoid as I can see from the last piece of your question. But on that note looks like you just need to get more comfortable Mongoid and how to actually create the relationships. When you create a relationship you do not manually put the ID into the model_id field. You actually just pass an entire saved saved model to the model you are creating the relationship on.

http://mongoid.org/en/mongoid/docs/relations.html

The above reference page should help a lot.

For your example you would want to do something like this: (I'm using Home class for examples sake)

a = User.create(:name => "y", {...})
b = User.create(:name => "user2", {...})
home = Home.create(:name => "x", {...})
home.users = [a, b]
a.homes.push(home)
b.homes.push(home)

There are possibly faster ways of doing this, and by that I mean fewer lines of code to be used, but I think this should help with the concept of how relationships work with Mongoid, and just to narrow down your searching in the docs, it looks like you're making a Referenced N-N relationship here ;)

bmeyers
  • 734
  • 4
  • 8