回答:
クラス向け
Class.column_names.include? attr_namewhere attr_nameは属性の文字列名を使用します。
この場合: Number.column_names.include? 'one'
インスタンスの場合
使用record.has_attribute?(:attr_name)またはrecord.has_attribute?('attr_name')(Railsの3.2以上)またはrecord.attributes.has_key? attr_name。
この場合:number.has_attribute?(:one)またはnumber.has_attribute?('one')またはnumber.attributes.has_key? 'one'
number.has_attribute?記号または文字列を受け付ける
userがuser_id、一部のモデルがユーザーを委任していたため、代わりに探す必要がありました。
Hash#has_key?の代わりに廃止されますHash#key?
インスタンスオブジェクトでは、defined? instance.attributeまたはも使用できますinstance.respond_to? :attribute。
これらは、モデル属性または任意のメソッドもチェックするためのより一般的なソリューションです。
instance.respond_to?(:attribute) == false ; instance.attribute ; instance.respond_to?(:attribute) == true
Hash#select:number_hash.select { |key, value| Number.column_names.include? key }