[英]Ruby on Rails 'find' question
I'm currently using: 我目前正在使用:
@user = User.find(params[:id])
@posts = Post.find(:all, :conditions => ["user_id = ?", @user.id])
@comments = Comment.find(:all, :conditions => ["user_id = ?", @user.id])
Which identifies the user, and lists all their comments they've made. 可以识别用户并列出他们的所有评论。 However, I want to set a local variable for use in the _comment.html.erb template.
但是,我想设置一个局部变量供_comment.html.erb模板使用。 I want the variable to list the post's name located in the 'name' column of the post table.
我希望变量列出位于帖子表的“名称”列中的帖子名称。
I tried doing: 我试着做:
@post_id = @comments.post_id
@post_name = @post_id.name
But it showed an array error (because @comments lists the array of user comments). 但是它显示了一个数组错误(因为@comments列出了用户注释的数组)。 I need to find a way to be able to find the post_id for EACH comment.
我需要找到一种方法来找到每个注释的post_id。 Yet when I try using something like
但是当我尝试使用类似
@post_id = @comments.each.post_id
It shows an error because it doesn't recognise 'post_id' as a method. 由于未将“ post_id”识别为方法,因此显示错误。 I want it to output whatever's in the post_id column for EACH comment.
我希望它输出post_id列中的内容以供每个注释使用。
You're going about this all wrong. 您要解决所有这些错误。 The idea is to use associations so rails will provide accessors for you.
这个想法是使用关联,因此rails将为您提供访问器。 You should only be using
find
to get your top-most record. 您应该只使用
find
来获取最高记录。 Rails will populate the associations for you. Rails将为您填充关联。
def User
has_many :posts
has_many :comments
end
def Post
belongs_to :user
end
Your code then becomes: 您的代码将变为:
@user = User.find(params[:id])
# you can omit these and just use @user.posts and @user.comments in your view
@posts = @user.posts
@comments = @user.comments
If you want to output something for each comment, then use a loop in your view 如果要为每个注释输出某些内容,请在视图中使用循环
<div class="comments">
<% @user.posts.each do |p| %>
<div class="post">
<%= p.body %>
</div>
<% end %>
</div>
class User
has_many :posts
end
class Post
belonds_to :user
has_many :comments
end
class Comment
belongs_to :post
end
<h1>users contriller</h1>
@user = User.includes(:posts).where(:id => params[:id]).first
<h1>in posts view</h1>
<div class="comments">
<% @user.posts.each do |p| %>
<div class="post">
<%= p.name %>
</div>
<% end %>
</div>
<h1>in comment view</h1>
<div class="comments">
<% @user.posts.each do |p| %>
<div class="post">
<%= p.comments.body %>
</div>
<% end %>
</div>
Don't issue queries yourself when you can leverage associations better. 当您可以更好地利用关联时,不要自己发出查询。
If a user has_many
posts, and a post has_many
comments, you can simply do. 如果用户
has_many
帖子,并且帖子has_many
评论,则可以轻松完成。
user.posts
and 和
post.comments
Also, it looks like a user has_many
comments, so you can directly do 另外,它看起来像用户的
has_many
注释,因此您可以直接执行
user.comments
To get the post id from a comment, make a comment belongs_to
a post, then you can do: 要从评论中获取帖子ID,请在评论中添加一条
belongs_to
帖子,然后可以执行以下操作:
comment.post.id # or comment.post.name directly
Post has many comments 帖子有很多评论
and comment belongs to post. 和评论属于帖子。 the should create the foreign key or the glue between post and comment.
应该在发布和评论之间创建外键或粘合剂。
remember that @user has all the post with the comment attached. 请记住,@ user的所有帖子都附有评论。
So to get to the comments id,go from @user.each.post({|post| 因此,要获取评论ID,请从@ user.each.post({| post |
post.comment} 发表评论}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.