rails 多个 .each 问题

rails multiple .each issue

我在我的 rails 中使用 mongoid,并且在 运行 多个 .each

中遇到一个小问题

我有 3 个模型:Users、`Places' 和 'Posts'

UsersPlaces has_many PostsPosts belongs_to UsersPlaces

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
}

我在我的用户模型上有一个方法来获取我朋友中的所有用户,包括我自己:

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

这个 returns 返回我所有朋友的数组,包括我自己。

我想做的是获取我的朋友和我自己的列表,在每个用户下都有一个他们的 post 的列表,并且在每个 post 中有一个参考它绑定的地方......大部分都在工作

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

唯一不起作用的部分是我的 :posts 上的 .each。我可以在日志中看到它正在搜索位置 table,但没有向集合中添加任何内容。

它添加了这个输出:

[
    {
        "_id": "556e26844a75730453170000",
        "authentication_token": "Q6R4FNs5i3n1-zfQfbp8",
        "created_at": "2015-06-02T21:56:20.684Z",
        "dob": "1982-08-01",
        "email": "test1@gmail.com",
        "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": "test@gmail.com",
        "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"
    }
]

您的问题不在于 each。它要么是 Place.find 要么是 resource[:posts]

尝试替换

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

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

---编辑----

替换

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

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

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

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