2012-05-17 73 views
0

我在一個名爲stream的實例變量中使用了一些JSON。我想用這樣的:Rails錯誤 - 屬性找不到

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: "[email protected]"> 

,我感到我現在用的屬性沒有發現錯誤。

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 

任何想法如何解決這個問題?

+0

你想做什麼? – Flexoid

+0

@Flexoid我想讓用戶實例變量反映json的用戶結構 – Awesomeness

+0

爲什麼你期望'for_user'在那裏?它是否在用戶模型中定義? –

回答

0

用戶= stream.try(:for_user)

的嘗試的方法是在這樣的地方可能不總是被填充的情況良好。

+0

這裏意外的'nil'沒有問題,所以這會導致完全相同的錯誤。 –

+0

@ moger777剛剛嘗試過,仍然有相同的錯誤NoMethodError:未定義的方法'for_user'爲#<用戶:0x131331918> – Awesomeness