Ruby on rails 如何在ActiveRecord的描述中编写查询

Ruby on rails 如何在ActiveRecord的描述中编写查询,ruby-on-rails,ruby-on-rails-5,rails-activerecord,Ruby On Rails,Ruby On Rails 5,Rails Activerecord,表: 我的sql查询: posts - id, title, description, vote_count post_votes - id, user_id, post_id, vote 我不熟悉RoR和ActiveRecord。有人能帮我在ActiveRecord中编写上面的查询吗 我正在使用Rails 5.2解决ActiveRecord中的上述查询: SELECT posts.*, (select vote from post_votes where post_votes.post_id

表:

我的sql查询:

posts - id, title, description, vote_count
post_votes - id, user_id, post_id, vote
我不熟悉RoR和ActiveRecord。有人能帮我在ActiveRecord中编写上面的查询吗


我正在使用Rails 5.2解决ActiveRecord中的上述查询:

SELECT posts.*, (select vote from post_votes where post_votes.post_id = posts.id and post_votes.user_id = 1) as my_vote FROM posts;
Post.select("posts.*, (select vote from post_votes where post_votes.post_id = posts.id and post_votes.user_id = 1) as my_vote").order('created_at DESC')