How to Specify a Relationship Column in ActiveRecord

Asked 2 years ago, Updated 2 years ago, 74 views

Post

  • id
  • title
  • body

Comment

  • id
  • body
  • post_id

When you combine these two in a relationship, you don't know how to specify columns to retrieve data.

For example, if I want to get Post.id, Post.title, Comment.id, Comment.body, how do I write select()?

ruby rails-activerecord

2022-09-29 22:01

4 Answers

Suppose the Post and Comment classes are defined like this.

class Post
  has_many —Comments
end

class comment
  belongs_to —post
end

This is what happens when you list a particular Post 1 and the associated Comments.

class PostController <ApplicationController
  def show
    @post=Post.find (params[:id])
  end
end

This is View.(views/posts/show.html.erb)

Title: <%[email protected]%>
Body: <%[email protected]%>
Comments:
<ul>
<%@post.comments.each do | comment | %>
<li><%=comment.body%>/li>
<%end%>
</ul>

If you want to get a list of multiple Posts and comments that are tied to each Post, this should be what it should look like.

 posts=Post.all# or Post.where(title: 'Today's Events') and more
posts.each do|post|
  id=post.id
  title=post.title
  body=post.body
  post.comments.each do | comment |
    comment_id=comment.id
    comment_body = comment.body
    post_id=comment.post_id
  end
end

By the way, I explained in detail how to create data for related models in Qiita a long time ago, so please refer to it if you like.

How to save relevant data (parent-child relationships) in Rails


2022-09-29 22:01

We will talk on the assumption that we already have a relationship (has_many, belongs_to association already mentioned). The relationship is explained in dot installation, so please refer to it. (http://dotinstall.com/lessons/basic_rails_v2)

In that case, for example, if you would like to comment on a post with an id of 1,

@post=Post.where(id=1)
@p ost.id // Here is the post ID.
@post.title// Here is the post ID.
@p ost.Comment.id // Here is the ID of the comment.
@post.Comment.body// Here is the body of the comment.

If you do this, you will be able to get what you need.


2022-09-29 22:01

Is it like the following?

@posts=Post.joins(:comments)
             .select('posts.id, comments.idascid, posts.body, comments.body')

The to_sql for this is as follows (I put the new line in).

SELECT posts.id, comments.id ascid, posts.body, comments.body ascbody 
  FROM "posts" INNER JOIN 
       "comments" ON "comments". "post_id" = "posts". "id"

The model is as follows:

class Post<ActiveRecord::Base
  has_many —Comments
end

class Comment <ActiveRecord::Base
  belongs_to —post
end


2022-09-29 22:01

"It's not a reference method, it's a retrieval method."
I think I would like to narrow it down at the time of the SELECT statement.

However,
when I got four of "Post.id, Post.title, Comment.id, Comment.body" I cannot determine if the .id for the retrieved instance indicates Post.id or Comment.id.

In general, let's say select('posts.id as...') like Mr.friedonion.
The area_table gives you more freedom.

p1=Post.arel_table ['id']
p2 = Post.arel_table ['title']
c1 = Comment.arel_table ['id'].as('comment_id')
c2 = Comment.arel_table ['body']
posts = Post.joins(:comment).select(p1, p2, c1, c2)
posts.first.body# No inclusion required as it is contained in row data

Of course Post.arel_table['*'] is also OK.

I don't use aarel_table often myself, so I wonder if I can add it to my wishes.
I'm a little worried that there might be a better way to use it.
If you pass the table name in a string and start thinking that periods and quotes are troublesome,
Consider using the area_table

※ We have confirmed that there is no problem in the activecord-sqlserver-adapter environment.


2022-09-29 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.