国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Ruby的25個編程細節(技巧、實用代碼段)

2019-10-26 19:26:57
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德阳市| 河东区| 红桥区| 古田县| 昭觉县| 黑龙江省| 莱西市| 闻喜县| 达日县| 贵阳市| 苍梧县| 西贡区| 苍南县| 漠河县| 皮山县| 沧州市| 孟津县| 民丰县| 广州市| 霍邱县| 鹿泉市| 上林县| 会泽县| 扶余县| 城固县| 绍兴县| 邛崃市| 长丰县| 镇康县| 商城县| 体育| 舒兰市| 龙山县| 梅州市| 阿克苏市| 昆山市| 西贡区| 垦利县| 中山市| 阳东县| 思茅市|