Rails的“任何?”实例方法回调?(Rails “any?” instance method callback?)

如果通过此'.any'电话找到帖子,如何收到帖子的ID?

<% if @posts.any? {|p| p.title == my_title} %>

How to receive the post's ID, if post is found via this '.any' call?

<% if @posts.any? {|p| p.title == my_title} %>

最满意答案

你应该这样做:

<% if (post = @posts.detect {|p| p.title == my_title} ) %> Post ID: <%= post.id %> <% end %>

You should probably do it like this:

<% if (post = @posts.detect {|p| p.title == my_title} ) %> Post ID: <%= post.id %> <% end %>

更多推荐