国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青岛市| 孙吴县| 东山县| 肃南| 阿拉善右旗| 永昌县| 安达市| 吴江市| 乌审旗| 耒阳市| 万山特区| 犍为县| 资中县| 舟曲县| 高邮市| 涟源市| 柞水县| 玉田县| 河东区| 台中县| 池州市| 哈巴河县| 山东| 富顺县| 湛江市| 寻乌县| 岚皋县| 新宁县| 朝阳市| 崇礼县| 雅江县| 上杭县| 湖南省| 报价| 馆陶县| 柏乡县| 绥德县| 郓城县| 曲沃县| 元谋县| 甘孜|