Railsのメカニズムを完全に誤解しました...多くの特性を持つ製品モデルを想像してみてください。
class Product < ActiveRecord::Base
has_many :properties
end
次に、コンソールで次のように入力します。
p=Product.last #recover the last product created
arr=p.properties #return the properties in an Array
arr.class #return "Array", so it's effectively an Array object.
Hirbではそれは私に与えます:
1.9.3-p385 :161 > arr=p.properties
| id | name | presentation | created_at | updated_at | value_type |
+-----------+-------------+---------------+-------------------------+-------------------------+------------+
| 905834907 | internet | internet | 2012-09-17 13:37:57 UTC | 2012-10-02 15:46:37 UTC | boolean |
| 905834906 | three_d | 3D | 2012-09-17 13:37:47 UTC | 2012-10-10 13:10:07 UTC | boolean |
| 161337574 | brand | Marque | 2012-05-22 14:13:04 UTC | 2013-03-26 16:12:12 UTC | string |
等...
次に、私がそうする場合:
1.9.3-p385 :162 > arr.where(:value_type => "boolean")
Spree::Property Load (0.8ms) SELECT "spree_properties".* FROM "spree_properties" INNER JOIN "spree_product_properties" ON "spree_properties"."id" = "spree_product_properties"."property_id" WHERE "spree_product_properties"."product_id" = 1060500665 AND "spree_properties"."value_type" = 'boolean'
+-----------+----------+--------------+-------------------------+-------------------------+------------+
| id | name | presentation | created_at | updated_at | value_type |
+-----------+----------+--------------+-------------------------+-------------------------+------------+
| 905834907 | internet | internet | 2012-09-17 13:37:57 UTC | 2012-10-02 15:46:37 UTC | boolean |
| 905834906 | three_d | 3D | 2012-09-17 13:37:47 UTC | 2012-10-10 13:10:07 UTC | boolean |
| 905834914 | wifi | wifi | 2013-03-26 16:13:35 UTC | 2013-03-26 16:13:35 UTC | boolean |
だから私は配列でwhereメソッドを実行します...しかし:
tab.method(:where) #returns:
NameError: undefined method `where' for class `Array'
それを認識しないオブジェクトのどこを実行するにはどうすればよいですか?私はある種の考えを持っています:
1.9.3-p385 :164 > arr.klass
=> Spree::Property(id: integer, name: string, presentation: string, created_at: datetime, updated_at: datetime, value_type: string)
しかし、私はそのメカニズムを本当に理解していません...それはオブジェクト指向言語ではまったく新しいものです。
説明ありがとうございます。
PH