rails multiple .each issue

110 views Asked by At

I am using mongoid with my rails and am having a slight issue with running multiple .each

I have 3 models: Users, `Places', and 'Posts'

Users and Places both has_many Posts and Posts belongs_to both Users and Places

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip
  has_many :posts
}
class Place
  include Mongoid::Document
  include Mongoid::Timestamps
  has_many :posts
}
class Post
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paperclip

belongs_to :place
belongs_to :user
}

i have a method on my user model to get all the users within my friends, including myself like so:

def self.as_objects_with_self(id)
    friends = Array.new
    self.as_ids_with_self(id).each do | friend |
      friends << User.find(friend)
    end
    friends

  end

This returns back an array of all my friends including myself.

What I am trying to do, is get a list of my friends and myself, and under each user have a list of their posts, and within each post have a reference to the place it is tied to... most of this is working

resources = Friendship.as_objects_with_self(current_user.id)
resources.each do |resource|
  if resource.posts.count > 0
    resource[:posts] = resource.posts.all

    resource[:posts].each do |post|
      post[:place] = Place.find(post[:place])
    end
  end
end

The only part to this that isn't working, is the .each on my :posts. I can see in the logs that it's searching the place table, but nothing is being added to the collection.

It adds this output:

[
    {
        "_id": "556e26844a75730453170000",
        "authentication_token": "Q6R4FNs5i3n1-zfQfbp8",
        "created_at": "2015-06-02T21:56:20.684Z",
        "dob": "1982-08-01",
        "email": "[email protected]",
        "gender": "Male",
        "last_action": null,
        "name": "tester1",
        "picture_content_type": null,
        "picture_file_name": null,
        "picture_file_size": null,
        "picture_fingerprint": null,
        "picture_updated_at": null,
        "picture_url": "/pictures/original/missing.png",
        "posts": [
            {
                "_id": "556e32b54a75730453270000",
                "created_at": "2015-06-02T22:48:21.962Z",
                "media_content_type": null,
                "media_file_name": null,
                "media_file_size": null,
                "media_fingerprint": null,
                "media_updated_at": null,
                "picture_content_type": "image/jpeg",
                "picture_file_name": "8580243774_479d5fe7bf_z.jpg",
                "picture_file_size": 191938,
                "picture_fingerprint": "61bdc1d21158c76d601b028bf826b437",
                "picture_updated_at": "2015-06-02T22:48:21.768+00:00",
                "place_id": "556cd5dc4a75730453010000",
                "type": "picture",
                "updated_at": "2015-06-02T22:48:21.962Z",
                "user_id": "556e26844a75730453170000"
            },
            {
                "_id": "556e351f4a75730453280000",
                "created_at": "2015-06-02T22:58:39.761Z",
                "media_content_type": null,
                "media_file_name": null,
                "media_file_size": null,
                "media_fingerprint": null,
                "media_updated_at": null,
                "picture_content_type": "image/jpeg",
                "picture_file_name": "8580243774_479d5fe7bf_z.jpg",
                "picture_file_size": 191938,
                "picture_fingerprint": "61bdc1d21158c76d601b028bf826b437",
                "picture_updated_at": "2015-06-02T22:58:39.571+00:00",
                "place_id": "556cd5dc4a75730453010000",
                "type": "picture",
                "updated_at": "2015-06-02T22:58:39.761Z",
                "user_id": "556e26844a75730453170000"
            }
        ],
        "telephone": "5555555555",
        "thumb_picture_url": "/pictures/thumbnail/missing.png",
        "updated_at": "2015-06-02T21:56:20.705Z",
        "username": "tester1"
    },
    {
        "_id": "556cd5934a75730453000000",
        "authentication_token": "RErefcuH5eDsSaNw6gCB",
        "created_at": "2015-06-01T21:58:43.198Z",
        "dob": "1982-08-01",
        "email": "[email protected]",
        "gender": "Male",
        "last_action": null,
        "name": "tester",
        "picture_content_type": null,
        "picture_file_name": null,
        "picture_file_size": null,
        "picture_fingerprint": null,
        "picture_updated_at": null,
        "picture_url": "/pictures/original/missing.png",
        "telephone": "5555555555",
        "thumb_picture_url": "/pictures/thumbnail/missing.png",
        "updated_at": "2015-06-01T21:58:43.200Z",
        "username": "tester"
    }
]
1

There are 1 answers

4
makhan On BEST ANSWER

Your issue is not with each. It's either with Place.find or with resource[:posts]

Try to replace

post[:place] = Place.find(post[:place])

with

post[:place] = Place.find(post[:place_id])

--- edit ----

replace

resource[:posts].each do |post|
  post[:place] = Place.find(post[:place])
end

with

resource[:posts].map! do |post|
  post[:place_id] = Place.find(post[:place_id])
  post
end

or

resource[:posts].each.with_index do |post, i|
  resource[:posts][i][:place] = Place.find(post[:place_id])
end

or

resource.posts.each do |post|
  post[:place] = Place.find(post.place)
end