To select controls the columns of the models associated with rails

Asked 1 years ago, Updated 1 years ago, 64 views


when you want to limit which columns are retrieved using select How do I control the columns of the relevant model as well?

Post>--Category

I only want to get title for Post and name for Category.

Image
Post.select(:title,:category=>name)

Post>-<Tag

I only want to get title for Post and Tag name.

Image
Post.select(:title,:tags=>name)

ruby-on-rails ruby rails-activerecord

2022-09-30 16:23

1 Answers

Post.joins(:category).select('posts.title, categories.name')

Post.joins(:tag).select('posts.title, tags.name')

You can use a string instead of a key.


2022-09-30 16:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.