Skip to content
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

feature request: associations #4

Open
drewda opened this issue Jan 8, 2015 · 3 comments
Open

feature request: associations #4

drewda opened this issue Jan 8, 2015 · 3 comments

Comments

@drewda
Copy link

drewda commented Jan 8, 2015

Do you plan to support associations among models?

@Nedomas
Copy link
Owner

Nedomas commented Jan 9, 2015

Thanks for asking. I've been thinking a lot on how should we deal with associations, as it is quite common in my production code.

But it is not clear what is the best way to do that. I was thinking of doing something like this:

User.find(1).then(function(user) {
  // some extended array [{ id: 3, user_id: 1, title: 'Hello' }]
  print(user.posts);

  user.posts.create({ title: 'Yo' }).then(function(post) {
    // { user_id: 1, title: 'Yo' }
    print(post);
  });
});

But then there are moments of:
1 - Should we always preload all the associations or have then on demand

user.posts().then(...

2 - How about creating model and giving the association as a property?

Post.create({ user: the_user, title: 'Hello' })

3 - How do you specify the association endpoints on the API? Or should it just load the associations on models by calling .posts on them?

// now we do
var User = new Databound('users');
var Post = new Databound('posts');

// then should we specify the associations alongside
var Post = new Databound('posts');
var User = new Databound('users', post: Post);

// or have it auto-discover the endpoints by sending one request on load 
// or having the first request to the backend be a bit slower than usual

Got any ideas on how to deal with those issues? I would love to work together and implement that into our next release.

Thanks ❤️ ❤️ ❤️

@Nedomas
Copy link
Owner

Nedomas commented Jan 10, 2015

Just thought a bit more about that and realised that I am already doing this in the production app I work on.

You can override the as_jsonmethod on the parent model. Something like that:

# in user.rb
def as_json(*)
  super.merge(posts: posts.to_json)
end

# in JS
User.find(1).then(function(user) {
  // [{ id: 15... }]
  print(user.posts)
});

But a much better way is to use Active Model Serializers to do the serialisation and embed the associations there.

The questions now really are:

1 - Should this library start managing serialisation (viewable_columns)? What about the security aspect of it?

2 - Should we provide an API for associations? (think user.posts().then)? Where do you specify the relation permissions then? Just an idea below:

class UsersController < ApplicationController
  databound do
    model :user

    has_many :posts do
      model :post
    end
  end
end

Lets try to come up with a simple solution and implement it only then. Lets not build a monster.

@drewda
Copy link
Author

drewda commented Jan 11, 2015

This sounds great. Yes, probably best to avoid "monsters" like Ember-data or Backbone-relational.

Re your specific questions:

  1. When customization of viewable columns and other more detailed aspects is necessary, I think it's reasonable to have developers create an AMS serializer for the time being.
  2. Looks good. Good that that will support situations where the association has a name different than the model.

@Nedomas Nedomas added this to the v3 milestone Jul 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants