Rails Active record selection

Q: all posts and related comments in one request, loading comment relationships in p.comments, but select only the title from the comments

Message

#db
create_table :posts do |t|
  t.string :title
  t.text :text

  t.timestamps
end

#modlel
class Post < ActiveRecord::Base
  has_many :comments
end

Comment

#db
create_table :comments do |t|
  t.string :title
  t.text :text
  t.integer :post_id

  t.timestamps
end

#model
class Comment < ActiveRecord::Base
  belongs_to :post
end

What I need: Select all posts and related comments in one request and load the comment relation to p.comments, but select only the header from the comments:

posts = Post.includes(:comments).select("posts.*, comments.title") 
posts.each do |p|
    puts(p.title)
    puts(p.text)
    p.comments.each do |c| 
        puts(c.title)
    end
end

If the first part is done using .includes:

posts = Post.includes(:comments).select('posts.*, comments.title')
posts.first.comments.loaded? # true

= > SELECT posts. id AS t0_r0, posts. title AS t0_r1, posts. text AS t0_r2, posts. created_at AS t0_r3, posts. updated_at AS t0_r4, comments. id AS t1_r0, comments. title AS t1_r1, comments. text AS t1_r2, comments. post_id AS t1_r3, comments. created_at AS t1_r4, comments. updated_at AS t1_r5 FROM posts LEFT OUTER JOIN comments comments. post_id= posts. id

.

, :

Post.joins(:comments).select('posts.*, comments.title')

= > SELECT posts. *, comments.title FROM posts INNER JOIN comments comments. post_id= posts. id

, - Rails (?)

posts = Post.joins(:comments).select('posts.*, comments.title as comments_title').first

= > SELECT posts. *, comments.title comments_title FROM posts INNER JOIN comments comments. post_id= posts. id

posts.first.comments.loaded? # false
posts.first.comments_title # "Comment 1.1"

, AREL

p = Post.arel_table
c = Comment.arel_table
posts = Post.find_by_sql(
    p.join(c).on(c[:post_id].eq(p[:id])) \
     .project(p[:id],p[:title],c[:id],c[:title]).to_sql
)

= > SELECT posts. id, posts. title, comments. id, comments. title FROM posts INNER JOIN comments comments. post_id= posts. id

- ActiveRecord .

?

UPD

  • , , AR . AR AREL, find_by_sql , , , , : .

  • , " "?

    posts = Post.select('posts.id, posts.title, comments.id, comments.title'). (: ) posts.first.comment.loaded? #true

= > SELECT posts. id AS t0_r0, posts. title AS t0_r1, posts. text AS t0_r2, posts. created_at AS t0_r3, posts. updated_at AS t0_r4, comments. id AS t1_r0, comments. title AS t1_r1, comments. text AS t1_r2, comments. post_id AS t1_r3, comments. created_at AS t1_r4, comments. updated_at AS t1_r5 FROM posts LEFT OUTER JOIN comments comments. post_id= posts. id

+3
3

, , .

  • : includes = >
  • : joins = >

, , , include, .

, - , SQL. , select include .

, , , , except . , .

, include, , comment.text. , .

+3

SQL- , :

post_comment = Post.find_by_sql("SELECT p.*, c.title AS comment_title FROM posts p JOIN comments c ON p.id=c.post_id").first
post_comment.comment_title

, , , . ActiveRecord . http://apidock.com/rails/ActiveRecord/Base/connection

0

DataMapper ActiveRecort. , AR. DataMapper ActiveRecord

-1

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


All Articles