-
Notifications
You must be signed in to change notification settings - Fork 4
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
Associations #30
Associations #30
Conversation
…ure this is a great idea)
You could add class User
has_many :posts #... w/e
has_many :attached_medias, through: :posts
end
# With an AR-style interface:
User.includes(posts: :attached_medias).all
# => GET /api/users?includes=posts.attached_medias
# or from an instance:
user = User.find(123)
user.attached_medias
#=> GET /api/posts?user_id=123&includes=attached_medias |
Sure, but i also never want to make |
Also: this assumes that there's a certain amount of compatible infrastructure in place which there may not be. |
end | ||
|
||
def test_belongs_to_works_with_expicit_class | ||
Account::Client::PartnerUser.stub :find, JsonApiClient::ResultSet.new([User.new(id: 2)]) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's an explicit class vs a implicit class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whether the class is user specified or derived from the association name, like
# implicit
has_one :address
#explicit
has_one :specialty, class: LawyerSpecialty
adding an
ActiveRecord
-like interface for basic associations.the general interface is
#{association} name, opts= {}
where the target class and foreign key is by default figured out from the
name
, but can be overridenassumptions
find
) and index(where
)where
) action has to supportskip_pagination
. Otherwise you may lose associations as they get capped by the per-page limitassocaitions
belongs_to
- your resource has the#{object}_id
for the association.object_class.find
has_one
- will call the server withopts.merge "#{root_class}_id => root_object.id
and grab the first thing that comes backobject_class.where
has_many
- same ashas_one
but will give you the full array of objectsoptions
:foreign_key
- this is what the server will get as the key forhas_one
andhas_many
.belongs_to
is weird and will send the key to the resource object. for example:action
- If you have a custom action you're using for lookup, you can override the defaultwhere
andfind
. For exampleNOTE: keep in mind, this will still make the call with the
opts.merge foreign_key => root_object.id
hash. If you want to override the query, you may want to consider 1: if the API is RESTful 2: rolling your own association.:prefetched_ids
(has_many
only) - in the case that the root object has a collection of ids that come preloaded in itnotes
:through
is not supported, nor will it ever be, because of the hidden complexity and cost of n HTTP calls.Preloader
Sometimes you have many objects that you need to fetch associations for. It's expensive to have to iterate over them one by one and annoying to have to assign the results. Well, now there's a
Preloader
that can do all of that for youexample
Let's say you have
user
s who haveaddress
es.With the associations in place, you can now use them to preload all the addresses for your users in a single query.
params
The
Preloader
takes 2 parameters@users
in our example)notes
This is a simple tool so don't expect too much magic. The Preloader will explode if