What have you found for these years?

2008-08-13

rails 判斷是 proxy 還是 array?

updated:
靠,終於解決了,看 source code 啥都有寫,文件啥屁都沒寫...
就暫時這樣吧:

pre.respond_to?(:loaded?)

爛方法,但至少「目前」work


updated:
script/console 測試可以用,實際跑起來無效...
改用 respond_to? :sum 發現 array 跟 proxy method 根本完全一樣...
ok, 那用 __metaclass__.to_s =~ /ActiveRecord/ 總行吧?
console 試 ok, 結果實際跑起來告訴我:

undefined method `__metaclass__' for #<Array:0x2ab2be41b338>

來自:

/usr/lib64/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations/association_proxy.rb:177:in `send'

非常好!太棒了!這是在幹嘛??


updated:
棍,array 不回應 __is_a__... orz
幫他追加好了... 真是慘啊,只能用一堆怪招繞過 rails

*

User.first.photos.__is_a__ ActiveRecord::Associations::AssociationProxy

真討厭... 有沒有更好的方法?
有時候就是需要判斷啊,Array 跟 Proxy 明明就不同...
混在一起就不對了...

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0