开发者

In mapping arrays I am getting, "undefined method `map!'"

开发者 https://www.devze.com 2023-03-15 07:10 出处:网络
I am attempting to learn how to map arrays. I can\'t seem to translate this into what I need. The first array @fbc has a field \"user_id\" which refers to \"id\" in the User model. I don\'t need to ke

I am attempting to learn how to map arrays. I can't seem to translate this into what I need. The first array @fbc has a field "user_id" which refers to "id" in the User model. I don't need to keep the original array so I am using "map!" instead of "map". Instead of creating an array of all users and then mapping, I'm going directly to the database.

In FbComments model:

belongs_to :user

In User model:

开发者_开发知识库has_many :fb_comments

This is my best attempt at getting this to work:

@fbc = FbComments.where(:reviewee_id => current_user.id)
@fb_comments = User.map! { |u| [u, @fbc.find_by_id(u.user_id)] }

The error this produces:

undefined method `map!' for #<Class:0x000001049a1078>

Not sure of my syntax in the view:

<% @fb_comments.each do |fb| %>
   <%= fb[1].name %>: <%= fb[0].comment %>
<% end %>

Thanks!


You need to call map! on an array. User is a class, not an array, and (assuming it is an AR::Base subclass) it has no 'map!' method defined on it.

Call User.all.map! {...}, assuming this is what you are trying to do.

@fb_comments = User.all.map! { |u| [u, @fbc.find_by_id(u.user_id)] }
                    ^
0

精彩评论

暂无评论...
验证码 换一张
取 消