0

I have two tables which associated with each other. My first table is Company and other one is Entry. Entries has one Company and Companies has many entries. The foreign keys are entry_id and company_id.

I'm trying to retrieve the query data as JSON in AJAX request and this is the last query that I can do so far:

Company.find(params[:id]).entries

However it only returns the entries of course. I want to is some kind of : {'id':'1', 'entries':[]}

How can I achieve this?

1 Answer 1

2

You can render nested objects specifying it explicitely via the #to_json method:

@company = Company.find(params[:id])
render json: @company.to_json(include: :entries)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.