Reputation: 96
I am getting undefined method #merge
however #merge
method works good in the second block. What's wrong with Block 1?
# Block 1
network_posts = []
@networks.each do |network|
network_posts << network.posts.as_json.merge('pic' => network.pic.url)
end
# Block 2
network = []
@networks.each do |network|
network << network.as_json.merge('pic' => network.pic.url)
end
Upvotes: 0
Views: 4413
Reputation: 211610
The as_json
method is not obligated to return a Hash. It can return anything it wants, a string, a number, a boolean value, or even, as in this case, an array. Assuming it supports merge
is a mistake.
Since this appears to be operating on a collection (network.posts
) that will be an array. Merging that in isn't practical. You could merge in on each record's as_json
result:
network.posts.map do |post|
post.as_json.merge(...)
end
Upvotes: 2