轨道多个.each问题

时间:2015-06-05 05:43:11

标签: ruby-on-rails mongoid

我正在使用mongoid和我的rails一起运行多个.each

时出现轻微问题

我有3个模特:Users,`地方'和'帖子'

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

这会返回包括我在内的所有朋友的数组。

我想要做的是获取我的朋友和我自己的列表,并且在每个用户下面都有他们的帖子列表,并且在每个帖子中都有对它所绑定的地方的引用...大多数这是有效的

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

唯一不起作用的部分是我.each上的:posts。我可以在日志中看到它正在搜索地方表,但没有任何东西被添加到集合中。

它添加了这个输出:

[
    {
        "_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"
    }
]

1 个答案:

答案 0 :(得分:0)

您的问题不在于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