#ruby-on-rails #postgresql
#ruby-on-rails #postgresql
Вопрос:
У меня есть три таблицы
Table name: follows
#
# id :bigint not null, primary key
# following :bigint
# requestor :bigint
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
#
# Indexes
#
# index_follows_on_user_id (user_id)
belongs_to(
:user,
class_name: 'User',
foreign_key: 'user_id',
inverse_of: :follows
)
User
has_many(
:follows,
class_name: 'Follow',
foreign_key: 'user_id',
inverse_of: :user
)
Post
belongs_to(
:user,
class_name: 'User',
foreign_key: 'user_id',
inverse_of: :posts
)
Столбец запроса принимает user.id .
Я хотел бы показать только сообщения, в которых атрибут requestor == current_user.id . Могу ли я сделать это только с помощью join или есть способ сделать это с помощью оператора where?
Ответ №1:
joins
Здесь наиболее целесообразно использовать A:
Post.joins(users: :follows).where(follows: { requestor: current_user.id })
Вы могли бы избежать joins
этого с помощью подзапросов, но это не имело бы особого смысла.