0

我在一个名为 stream 的实例变量中使用了一些 JSON。我正在尝试像这样使用它:

user = stream.for_user # user model

这是流的内容:

=> #<User _id: 4f8de8c40a5dcd0d1d00004c, agency_requested: [], encrypted_password: "$2a$10$FPVJDLkkD6AXd5SY/hL6KeuC6QR6vfU3OKI3oGCPxrTiEnAwvGOOu", avatar_file_size: nil, avatar_file_name: nil, agreed_to_terms: ["v1"], location: nil, last_name_first_initial: "Genadinik, A.", first_initial_last_name: "A. Genadinik", legacy_blogger_id: nil, created_at: Tue Apr 17 22:03:50 UTC 2012, active_advertiser_id: nil, avatar_source: :default, agency_roles: [], last_sign_in_ip: "127.0.0.1", confirmation_sent_at: Tue Apr 17 22:03:50 UTC 2012, created_via: "CMPLY-WEB", deleted_at: nil, updated_at: Fri May 11 16:37:16 UTC 2012, last_sign_in_at: Thu May 10 20:48:53 UTC 2012, display_welcome_help: true, program_influencers: [], agency_rep: true, account_type: :influencer, legacy_user_id: nil, sign_in_count: 44, _type: nil, avatar_content_type: nil, programs: [], active_agency_id: nil, account_status: :active, admin_roles: [], legacy_password: nil, gets_newsletter: false, full_name: "Alex Genadinik", last_name: "Genadinik", reset_password_token: nil, current_sign_in_ip: "127.0.0.1", authentication_token: "ViQ5q89n39zyyVUT3wLp", user_name: "me_n0_like_scrennames", time_zone: "Chihuahua", bio: nil, reset_password_sent_at: nil, current_sign_in_at: Fri May 11 16:37:16 UTC 2012, confirmation_token: nil, avatar_updated_at: nil, last_name_first_name: "Genadinik, Alex", agency_id: nil, first_name: "Alex", confirmed_at: Tue Apr 17 22:49:28 UTC 2012, email: "alex@cmp.ly">

我收到一个错误,指出我正在使用的属性未找到。

NoMethodError: undefined method `for_user' for #<User:0x138be12a0>
    from /Library/Ruby/Gems/1.8/gems/mongoid-2.4.5/lib/mongoid/attributes.rb:166:in `method_missing'
    from /Users/alexgenadinik/projects/cmply/cmply-app/lib/app/monitoring/parsers/linked_in_parser.rb:40:in `fetch!'
    from (irb):18

知道如何解决这个问题吗?

4

1 回答 1

0

用户 = stream.try(:for_user)

try 方法在这种可能并不总是被填充的情况下很好。

于 2012-05-17T18:30:50.680 回答