Let’s assume I have the following three objects:
class Filing < ActiveRecord::Base
belongs_to :company
has_many :people , :dependent => :destroy
end
class Company < ActiveRecord::Base
has_many :filings
end
class Person < ActiveRecord::Base
belongs_to :filing
end
I’m attempting to make a single request that returns the filing with it’s associated company and an array containing it’s associated people. This is being used for an API request of GET filing/:id
I saw the documentation on joining tables with ActiveRecord, however when I run the following query:
Filing.joins(:people,:company)
It doesn’t appear to include the associated people or company in the result set. I’m kind of confused about why I would do a join if the associated data won’t be returned. What am I missing here? What query should I be running?
Update
As mentioned in the comments. I want to be able to generate the following output:
{ "filing" => { "filing_id" => 123, "company" => { ... }, "people" => [{"person_id" => 1}, {"person_id" => 2}] } }
With the help of commenters I ended up finding the answer. All that needs to be called is: