开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜