1.try 永遠不會拋出異常 在 沒有的時候 返回 nil
代碼如下:
province_id = Province.find_by_name(prov).try(:id)
2.find(:first, :condotions) 方法 不言而與
代碼如下:
mobile_info = MobileInfo.find(:first, :conditions => ["mobile_num = ? ", mobile_num.to_i])
3.find(:all, :select, :conditions)
代碼如下:
support_amount_a = ProvinceMerchantChangeValue.find(:all, :select => "DISTINCT change_value_id",
:conditions => ["status = 1 and merchant_id = ? and province_id =? and channel_id in (select id from channels where status = 1)",
merchant_id, province_id]).map { |cv| cv.change_value_id }.compact
support_amount_s = ChangeValue.find(:all,:select => "price" ,:conditions => ["id in (?)", support_amount_a]) /
.map { |cv| cv.try(:price).to_i }.compact
4.發送post請求 可以在shell中執行
代碼如下:
curl -d "channel=中信異度支付&action_type=娛人節-手機充值&user_indicate=13911731997&original_amount=10000" http://xx.xxx.xxx:3000/search.json
5.Ruby 中純數據結構 ( Struct 與 OpenStruct )
講一下它倆之間的區別:
Struct 需要開頭明確聲明字段; 而 OpenStruct 人如其名, 隨時可以添加屬性
Struct 性能優秀; 而 OpenStruct 差點, 具體的性能差距可看這里:http://stackoverflow.com/questions/1177594/ruby-struct-vs-openstruct
Struct 是 Ruby 解釋器內置, 用 C 實現; OpenStruct 是 Ruby 標準庫, Ruby 實現
API 不同: Struct API 與 OpenStruct
6. MIme::Type.register
代碼如下:
Mime::Type.register "application/json", :ejson
config/initializers/mime_types.rb
7.config/initializers/secure_problem_solved.rb
代碼如下:
ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING.delete('symbol')
ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING.delete('yaml')
8.config/initializers/new_rails_default.rb
代碼如下:
if defined?(ActiveRecord)
# Include Active Record class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = true
# Store the full class name (including module namespace) in STI type column.
ActiveRecord::Base.store_full_sti_class = true
end
ActionController::Routing.generate_best_match = false
新聞熱點
疑難解答