her
her copied to clipboard
Configuring foreign key associations through nested attributes
Hi there!
First off, amazing gem. I've been diving through the source all tonight and have been thoroughly impressed :+1:.
I'm trying to create a belongs_to
, with a foreign key that lives inside of a nested attribute of an API response. It looks something like this:
{
"cats":[
{
"id":99346,
"owner":{
"id":9072
}
}
]
}
Ideally, I'd have access to owner_id
so that Her could use it's default assumption for the foreign_key
here, so that it knows to call out to the next /owners
endpoint. Unfortunately, Her is creating an Owner
object for me, with one attribute: :id
, rather than calling out to the next endpoint.
That being said, is it possible with Her associations to use a nested id so that I can do something like the following:
class Cat
include Her::Model
belongs_to :owner
end
class Owner
include Her::Model
end
I'm not sure if this is an appropriate place to put it, but something like this does accomplish what I'm looking to do:
module Her::Model::ORM
def fetch
self.class.find(id)
end
end
Cat.find(1).owner.fetch