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

Add the inverse of "many :in" #240

Open
bkeepers opened this Issue Apr 11, 2011 · 11 comments

Comments

Projects
None yet
8 participants
@bkeepers
Contributor

bkeepers commented Apr 11, 2011

Currently many-to-many associations are one-sided. MM should be able to handle the inverse of the many :in association.

For example:

class Book
  include MongoMapper::Document
  key :title
  key :author_ids, Array
  many :authors, :in => :author_ids
end

class Author
  include MongoMapper::Document
  key :name
end

In addition to being able to create authors from a book, you should be able to create and find books from an author. I don't have any suggestions for syntax for how it should be defined.

@brianhempel

This comment has been minimized.

Show comment
Hide comment
@brianhempel

brianhempel Apr 11, 2011

Contributor

I was thinking...

many :books, :foreign_array => :author_ids
Contributor

brianhempel commented Apr 11, 2011

I was thinking...

many :books, :foreign_array => :author_ids
@scottmessinger

This comment has been minimized.

Show comment
Hide comment
@scottmessinger

scottmessinger Apr 12, 2011

What about letting the value for in be a model and array key?

class Author
many :books, :in => Book.author_ids
end

scottmessinger commented Apr 12, 2011

What about letting the value for in be a model and array key?

class Author
many :books, :in => Book.author_ids
end

@hubertlepicki

This comment has been minimized.

Show comment
Hide comment
@hubertlepicki

hubertlepicki Apr 22, 2011

I was thinking if it's really needed to store association keys in both models, and I think it's not. The implementation would be much more complicated.

I think the association has to be two-way, but array with keys should sit on one, carefully selected side.

For example, if you have a Document and Tag models, and several millions Documents, you probably want to store foreign keys in Document model, to prevent your Tag model from getting too large.

I think the guys from Mongoid tried implementing many-to-many associations by placing foreign keys on both sides -- and IMHO it's still not working properly, has got issues with synchronization and it's simply pain to use after several months of development. Keep it on one side and keep it simple.

hubertlepicki commented Apr 22, 2011

I was thinking if it's really needed to store association keys in both models, and I think it's not. The implementation would be much more complicated.

I think the association has to be two-way, but array with keys should sit on one, carefully selected side.

For example, if you have a Document and Tag models, and several millions Documents, you probably want to store foreign keys in Document model, to prevent your Tag model from getting too large.

I think the guys from Mongoid tried implementing many-to-many associations by placing foreign keys on both sides -- and IMHO it's still not working properly, has got issues with synchronization and it's simply pain to use after several months of development. Keep it on one side and keep it simple.

@brianhempel

This comment has been minimized.

Show comment
Hide comment
@brianhempel

brianhempel Apr 22, 2011

Contributor

Oh, no doubt the keys will always be stored only on one side. The problem is that right now only one side has all the sugary methods to manage the relationships...MongoMapper lacks an association "proxy" for the side that doesn't store the keys.

e.g. I can do my_book.authors but I can't do my_author.books

Contributor

brianhempel commented Apr 22, 2011

Oh, no doubt the keys will always be stored only on one side. The problem is that right now only one side has all the sugary methods to manage the relationships...MongoMapper lacks an association "proxy" for the side that doesn't store the keys.

e.g. I can do my_book.authors but I can't do my_author.books

@hubertlepicki

This comment has been minimized.

Show comment
Hide comment
@hubertlepicki

hubertlepicki Apr 22, 2011

Agree. I was just looking at the MongoMapper code -- looks like we could create something like InForeignArrayProxy, similar to InArrayProxy, and initialize it with something like that in our models:

class Author
many :books, :in => {:class => Book, :key => :author_ids}
end

I guess both classes could share most of the test cases, and possibly also some source code.

The other thing I was wondering about is that, for things like checkboxes in forms, we could have virtual book_ids and book_ids= methods... but not entirely convinced about that myself.

hubertlepicki commented Apr 22, 2011

Agree. I was just looking at the MongoMapper code -- looks like we could create something like InForeignArrayProxy, similar to InArrayProxy, and initialize it with something like that in our models:

class Author
many :books, :in => {:class => Book, :key => :author_ids}
end

I guess both classes could share most of the test cases, and possibly also some source code.

The other thing I was wondering about is that, for things like checkboxes in forms, we could have virtual book_ids and book_ids= methods... but not entirely convinced about that myself.

@hubertlepicki

This comment has been minimized.

Show comment
Hide comment
@hubertlepicki

hubertlepicki Apr 23, 2011

ok, I spend an hour trying to get it working and came up with the following:
#259

would be good if someone could review that solution...

hubertlepicki commented Apr 23, 2011

ok, I spend an hour trying to get it working and came up with the following:
#259

would be good if someone could review that solution...

@wheelq

This comment has been minimized.

Show comment
Hide comment
@wheelq

wheelq Jul 19, 2014

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.

wheelq commented Jul 19, 2014

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.

@JonKernPA

This comment has been minimized.

Show comment
Hide comment
@JonKernPA

JonKernPA Jul 22, 2014

Contributor

see if any of these examples help: https://github.com/JonKernPA/mongo_examples

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.

Contributor

JonKernPA commented Jul 22, 2014

see if any of these examples help: https://github.com/JonKernPA/mongo_examples

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.

@JonKernPA

This comment has been minimized.

Show comment
Hide comment
@JonKernPA

JonKernPA Jul 22, 2014

Contributor

Now that I read your question a bit closer… And I was looking at this example. Where Event class has many such associations...

What use is storing a duplicate ID? It makes no sense.

If you truly need some additional information about the association, then you need to model that info in it’s own class and use something other than
many :userlist, class_name: 'User', :in => :user_id

For reference, you can google “Association Classes"

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.

Contributor

JonKernPA commented Jul 22, 2014

Now that I read your question a bit closer… And I was looking at this example. Where Event class has many such associations...

What use is storing a duplicate ID? It makes no sense.

If you truly need some additional information about the association, then you need to model that info in it’s own class and use something other than
many :userlist, class_name: 'User', :in => :user_id

For reference, you can google “Association Classes"

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.

@wheelq

This comment has been minimized.

Show comment
Hide comment
@wheelq

wheelq Jul 22, 2014

Not really :(

Sent from my iPhone

On 22 Jul 2014, at 11:57, Jon Kern notifications@github.com wrote:

see if any of these examples help: https://github.com/JonKernPA/mongo_examples

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

wheelq commented Jul 22, 2014

Not really :(

Sent from my iPhone

On 22 Jul 2014, at 11:57, Jon Kern notifications@github.com wrote:

see if any of these examples help: https://github.com/JonKernPA/mongo_examples

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

@wheelq

This comment has been minimized.

Show comment
Hide comment
@wheelq

wheelq Jul 22, 2014

In my case it makes sense. As i store tasks to do, so some tasks are repetitive like:

  • grab pen
  • put in the drawer
  • grab pencil
    -put in the drawer

Sent from my iPhone

On 22 Jul 2014, at 12:52, Jon Kern notifications@github.com wrote:

Now that I read your question a bit closer… And I was looking at this example. Where Event class has many such associations...

What use is storing a duplicate ID? It makes no sense.

If you truly need some additional information about the association, then you need to model that info in it’s own class and use something other than
many :userlist, class_name: 'User', :in => :user_id

For reference, you can google “Association Classes"

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

wheelq commented Jul 22, 2014

In my case it makes sense. As i store tasks to do, so some tasks are repetitive like:

  • grab pen
  • put in the drawer
  • grab pencil
    -put in the drawer

Sent from my iPhone

On 22 Jul 2014, at 12:52, Jon Kern notifications@github.com wrote:

Now that I read your question a bit closer… And I was looking at this example. Where Event class has many such associations...

What use is storing a duplicate ID? It makes no sense.

If you truly need some additional information about the association, then you need to model that info in it’s own class and use something other than
many :userlist, class_name: 'User', :in => :user_id

For reference, you can google “Association Classes"

Jon Kern

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA
github: https://github.com/JonKernPA

On Jul 19, 2014, at 8:22 AM, Michal Wiczynski notifications@github.com wrote:

Something doesn't work properly or maybe I am doing something wrong.
have a Document with Array of ObjectId:

Class Task
key :user_id, Array
many :userlist, class_name: 'User', :in => :user_id
In that Array I store different user_id values, sometimes duplicated. I can see duplicated user_id's using:

@task.user_id.each do |z|
puts z
end
But when I fetch and associate the data using:

@task.userlist.each do |z|
puts z.name
end
I do not get the duplicates :(, only unique id's get associated.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment