[英]How to write code in Rails to run my desired query in SQL
I'm making a chat room app in Rails and I have 3 tables as follows: 我正在Rails中制作一个聊天室应用,我有3张桌子,如下所示:
id
, name
用户id
, name
id
, user_id
, title
客房id
, user_id
, title
id
, room_id
, user_id
参与者id
, room_id
, user_id
users
and rooms
have the following associations. users
和rooms
具有以下关联。
# user.rb
has_many :rooms
# room.rb
belongs_to :user
I want to be able to show the list of names of participants by writing: 我希望能够通过以下方式显示参与者的姓名列表:
# rooms/show.html.erb
<% @participants.each do |participant| %>
<%= participant.user.name %>
<% end %>
In my controller, I have tried: 在我的控制器中,我尝试过:
# rooms_controller.rb
def show
@participants = Participant.includes(:users).where(room_id: 1)
end
But I get an error saying Association named 'users' was not found on Participant; perhaps you misspelled it?
但是我收到一个错误消息,说Association named 'users' was not found on Participant; perhaps you misspelled it?
Association named 'users' was not found on Participant; perhaps you misspelled it?
. 。
I have a feeling that setting associations between users
and participants
would might solve the problem, but I'm not sure what their relations are. 我感觉在users
和participants
之间设置关联可能会解决问题,但是我不确定他们之间的关系。 (It's not a simple has_many
/ belongs_to
relation, is it...?) (这不是一个简单的has_many
/ belongs_to
关系,是吗??)
Overall, I want to run a SQL query that's something like SELECT * FROM participants INNER JOIN users ON participants.user_id = users.id WHERE participants.room_id = 1
. 总体而言,我想运行一个类似SELECT * FROM participants INNER JOIN users ON participants.user_id = users.id WHERE participants.room_id = 1
的SQL查询SELECT * FROM participants INNER JOIN users ON participants.user_id = users.id WHERE participants.room_id = 1
。 How can I write in Rails to run this query? 如何编写Rails来运行此查询?
I think the relation between User
and Room
is a many-to-many. 我认为User
与Room
之间的关系是多对多的。 Then, use :has_and_belongs_to_many
: 然后,使用:has_and_belongs_to_many
:
https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
But if you want to set the relation between User
and Participant
as many-to-many, use option :through
: 但是,如果要将User
和Participant
之间的关系设置为多对多,请使用选项:through
:
https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
I think, a table Participant
is not required, just a temporary variable participants
seems ok. 我认为,不需要表Participant
,只需一个临时变量participants
。
Your participant
model does have an association name users
. 您的participant
模型确实具有关联名称users
。
You can set up a has many through
association like this: 您可以像这样has many through
关联建立一个has many through
:
# room.rb
belongs_to :user
has_many :participants
has_many :users, through: :participants
so in your controller you can get users of a room: 因此,在您的控制器中,您可以吸引一个房间的用户:
room = Room.find(1)
users = room.users
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.