Rails ActiveRecord: cut from multiple tables with the same column name

I have a simple example:

The user has many tweets. Tweet belongs to the user.

And I'm trying to wrest the column name that exists in both tables. For example:

@tweets = Tweet.includes(:user).all.pluck(:created_at)

Each table has a created_at column, but the result at the top returns created_at for the tweet. How can I also snatch user created_at?

My workaround is below using connections and selects:

@tweets = Tweet.joins(:user).select("users.created_at AS created_date").all

So how can I do this using pluck?

+4
source share
1 answer

You can do below

@tweets = Tweet.includes(:user).all.pluck(:created_at, "users.created_at")

.all , joins/includes / . ,

@tweets = Tweet.includes(:user).pluck(:created_at, "users.created_at")
+10

Source: https://habr.com/ru/post/1612936/


All Articles