databound icon indicating copy to clipboard operation
databound copied to clipboard

feature request: associations

Open drewda opened this issue 10 years ago • 3 comments

Do you plan to support associations among models?

drewda avatar Jan 08 '15 21:01 drewda

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 :heart: :heart: :heart:

Nedomas avatar Jan 09 '15 09:01 Nedomas

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.

Nedomas avatar Jan 10 '15 14:01 Nedomas

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.

drewda avatar Jan 11 '15 14:01 drewda