-
Notifications
You must be signed in to change notification settings - Fork 11
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
Comments
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: 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 // 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 ❤️ ❤️ ❤️ |
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 # 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 ( 2 - Should we provide an API for associations? (think 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. |
This sounds great. Yes, probably best to avoid "monsters" like Ember-data or Backbone-relational. Re your specific questions:
|
Do you plan to support associations among models?
The text was updated successfully, but these errors were encountered: