我怎样才能从对象中提取价值?

时间:2011-07-17 08:51:28

标签: ruby-on-rails

---
- !ruby/object:Comment
  attributes:
    post_id: '5'
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Comment
  attributes:
    post_id: '6'
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Comment
  attributes:
    post_id: '3'
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Comment
  attributes:
    post_id: '7'
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Comment
  attributes:
    post_id: '1'
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false

http://pastebin.com/J1angM6d

如何将post_id的值作为数组? @arr = [ 5,6,3,7,1 ] 现在我想用post_id获取这些帖子。 它是否正确 ? @p = Post.where(:id=>@arr)

1 个答案:

答案 0 :(得分:0)

@p = Post.find(my_collection.map(&:post_id))