Home > other >  How to return data from distinct tables into a JOIN relationship in ruby?
How to return data from distinct tables into a JOIN relationship in ruby?

Time:08-27

I have a relationship between Actor and Books. A Actor have n Books.

A Actor has a name.

A Book has a title.

A Actor called Fernando wrote 3 books called [ Book 1, Book 2, Book 3]

I want to make a query that returns:

[
{actor.name, book.title, 
{actor.name, book.title,...
]

Im my example it will returns

[ 
{Fernando, Book 1},
{Fernando, Book 2}, 
{Fernando, Book 3}
]

If I do a

Actor.joins(:books)

It will return only data from Actor.

How can I make a joins in ruby returning data from the 2 tables?

CodePudding user response:

try this...

Actor.joins(:books).select("actors.name, books.title")

CodePudding user response:

Solution:

Actor.joins(:books).select(:name, :title)
  • Related