her icon indicating copy to clipboard operation
her copied to clipboard

Configuring foreign key associations through nested attributes

Open bswinnerton opened this issue 9 years ago • 1 comments

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

bswinnerton avatar Nov 16 '14 03:11 bswinnerton

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

bswinnerton avatar Nov 17 '14 17:11 bswinnerton