Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
@mengqing
Member
NO. 13587 / 2014-06-11

[email protected]
1 Topics / 65 Replies
4 Followers
0 Following
1 Favorites
No GitHub.
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • Rails 为什么默认把 HEAD 请求当做 GET 来处理? at March 09, 2017

    因为 rack-cache 需要 content 来检测需不需要 refresh cache,而 head 是没有 content 的

    具体可以看这里 https://github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb#L198

  • 工程师只是负责实现吗? at March 01, 2017

    产品经理是负责产品的大势走向 工程师是负责技术上去实现 大家的分工不同 当然了一个小组里还有 technical leader。他/她的责任是支援产品经理的想法 和告知在技术上到底能不能实现和有没有必要实现。然后再分配工作

  • Ruby 里面有解密 MD5 的方法吗? at March 01, 2017

    MD5 是不能反向解密的 但是 MD5 会有冲撞,不同的数据有可能会创造出相同的 MD5 值,所以很久之前 MD5 就不被推荐来加密了

  • Amzon S3 出大乱子了 at March 01, 2017

    US-EAST-1

  • Amzon S3 出大乱子了 at March 01, 2017

    S3 没有 multi A-Z,不过可以 cross region replication。多花点钱而已

    还好这次只影响到了一个 region

  • 在有多个模型关联操作的情况下,使用 Callbacks 保证数据的一致性 at February 28, 2017

    你这里回滚了是因为你手动抛出了异常,save命令是不会抛出异常的,只会返回 true 或者 false。如果你需要抛出异常就要用save!

  • 找工作分享: 顺利通过 Code Test 的 Checklist at February 28, 2017

    @grantbb 上个月的 Rubyconf 你们公司有来吗?貌似好像看到过你们公司

  • 在有多个模型关联操作的情况下,使用 Callbacks 保证数据的一致性 at February 28, 2017

    感谢你的分享

    这里有一些问题我想指出来,希望可以帮到大家

    1. OrderID建议加个 index, 能加快 query 的速度
    2. after_destroy :destroy_all_same_orderID 没有必要,你只需要在Order里加一个OrderItem的关联,然后设置 destroy 的关系就可以了

      class Order < ApplicationRecord
        has_many :order_items, dependent: :destroy
      end
      
      class OrderItem < ApplicationRecord
        belongs_to :order, foreign_key: "OrderID"
      end
      
    3. 读取数据的时候不需要加self ,只有在写数据的时候需要

    4. after_create :create_order_items 里的 OrderItem 的创建没有检测到底成功了没有,如果出错了会导致 OrderItem 的遗失。而且因为你没有用 bang method, 所以出错了也不会有异常抛出,也就不会有回滚了

    我的一些建议

    1. 不要用 callbacks, 可以网上搜一下 rails callbacks hell,就能了解到 callbacks 带来的问题,而这些问题会随着业务逻辑的复杂而越来越难修正
    2. 不要把业务逻辑放在 model 里。业务逻辑可以放在 service objects 里,keep them isolated, easily testable, reusable, SRP
  • ExceptionTrack - 捕捉 Rails 应用运行期的异常,并存储到数据库 at February 18, 2017

    我们用 rollbar,不过还是给个赞

  • 使用 Elixir 的中国公司 at January 23, 2017

    @tony612 如果能把公司按城市区分那就更好了 能 remote 的公司还是比较少的

  • 2017 年想实现 Web 内的聊天,用什么技术方案比较好? at January 22, 2017

    我们用 Actioncable 实现了类似于 Whatsapp 类的聊天功能,不是非常的难。人不多的话没什么问题。scale 的话也没什么问题,但如果人多的话我不会选择用 Actioncable,太吃资源

  • 关于 Rails 的异常处理 at January 22, 2017

    问题 1

    There is no need to explicitly check whether the id exists or not. If you setup the routes correctly, then it would never have passed the routes if the required key does not exist.

    # routes.rb
    
    # products GET /products/:id(.:format) products#show
    resources :products
    

    In terms of exception rescuing on the controller level, like @huacnlee have said, you don't need to handle ActiveRecord::RecordNotFound as Rails has already handled it for you (rendering / returning 404), and you don't need to apply rescue_from in ApplicationController. If however you need to handle it differently in any particular actions, you can always rescue it per action.

    class ProductsController < ApplicationController
      def show
        @product = Product.find(params[:id])
      rescue ActiveRecord::RecordNotFound
        redirect_to :root, flash: { error: "产品不存在" }
      end
    end
    

    问题 2

    You can rescue exceptions in model, just like any of the ruby classes and methods. However, general practices is, you'll only ever need to rescue exceptions if you don't know whether it is going to raise exceptions or not (这不是废话嘛..), eg: user inputs, external sources, api responses, and even then it should be handled on a higher level, eg: controllers, service objects, form objects and validators etc.

    Issues like you've described, (NoMethodError) should have never made to production code base with unit testings. Thats why it is essential to have unit testing on each of the methods in your model.

  • 如何优雅的保存 Model 的最后更新者? at January 03, 2017

    https://github.com/airblade/paper_trail

  • ApplicationMailer 奇怪的调用方法 at January 03, 2017

    这是因为 actionmailer 的设计理念。因为 ActionMailer 的父类是一个 Controller,而 Controller 理论上只应该应对一个 action。ActionMailer 所以要防止你写这样的代码

    user_mailer = UserMailer.new
    user_mailer.foo
    user_mailer.bar
    
  • 被一个返回按钮折腾了两个小时 at December 08, 2016
    <%= submit_tag "返回", type: :button, onclick: "history.back()" %>
    
  • 怎样用较低的薪水雇佣程序员 at December 08, 2016

    选择是双向的 公司压低工资那就不要去 员工要太多工资那就不要请

  • 请问 Ruby 语言里面有弱引用的结构么? at December 07, 2016

    https://ruby-doc.org/stdlib-2.3.3/libdoc/weakref/rdoc/WeakRef.html

    可以自定义

  • Rails 怎么实现不同的域名访问的页面不同 at December 05, 2016

    直接在 routes 里可以实现 http://guides.rubyonrails.org/routing.html#request-based-constraints

    namespace :ht do
      constraints subdomain: 'ht' do
        # backend resources
      end
    end
    
    namespace :qt do
      constraints subdomain: 'qt' do
        # frontend resources
      end
    end
    
  • 找工作分享: 顺利通过 Code Test 的 Checklist at November 28, 2016

    lz 在澳洲哪个公司就职?

  • 关于 Redmine 的二次开发中遇到的 call_hook 这个方法 at November 22, 2016

    这是为了让第三方开发者在 plugin 里注册 callback functions,然后 redmine 会在某个定义的阶段运行那些 functions。

    你可以看下 redmine hooks 的官方 wiki,http://www.redmine.org/projects/redmine/wiki/Hooks

  • [讨论] 大量 String 判断式的重构方法 at November 15, 2016

    当涉及到大量 String 的时候,记得用 Set,O(1) vs O(n)

    platforms = Set.new %w[bash Chai D3JS Go Javascript Ruby]
    
    foo if platforms.include? platform
    
  • includes 之后,AR 速度反而降低? at November 09, 2016

    IN is generally equivalent to a list of OR for most database engines, and when you have a big list of OR, it generally makes the query unsargable (不优化搜索), and switches to full scan. In such situations, it is more sensible to use join instead.

    To use join and to avoid N+1, you can use references (http://apidock.com/rails/ActiveRecord/QueryMethods/references), which generates a LEFT JOIN

  • 成都有什么好吃好玩? at October 07, 2016

    致民路好多吃串串的地方

  • erb 中能否直接写 sytle? at October 03, 2016

    可能一开始你觉得没有什么必要,但是为了代码的清洁度,设一个 class 是很有必要的

    # foo.erb
    <span class="red">红</span>
    
    # foo.css
    .red { color: red; }
    
  • 有关 Rails 路由设计的最佳实践 at October 03, 2016

    这个可以在 routes 里实现,前提是可以在 session 里提取到用户的信息,如果你用的是 devise 或者 warden,用户信息是可以在 session 里提取的。然后可以用 constraints 来 match。

    # routes.rb
    constraints(TeacherConstrainer.new) do
      namespace :teacher do
        resource :dashboard
      end
    end
    
    constraints(StudentConstrainer.new) do
      namespace :student do
        resource :dashboard
      end
    end
    
    # teacher_constrainer.rb
    class TeacherConstrainer
      def matches?(request)
        # 首先需要确定是不是已经登录了
        return false unless request.env["warden"].authenticate?
    
        user = request.env["warden"].user
    
        # 自定义逻辑来确定用户是不是Teacher, eg: Rolify
        user.has_role? :teacher
      end
    end
    
    # student_constrainer.rb
    class StudentConstrainer
      def matches?(request)
        # 首先需要确定是不是已经登录了
        return false unless request.env["warden"].authenticate?
    
        user = request.env["warden"].user
    
        # 自定义逻辑来确定用户是不是Student, eg: Rolify
        user.has_role? :student
      end
    end
    
  • 进入 rails console 报 ruby 解释器错误 at September 30, 2016

    正在用 10.12 和 ruby 2.3.1 完全没问题。试试先 spring stop?

  • Gzip 压缩之后如何解压 at September 27, 2016

    如果用 Rails 的话可以用 ActiveSupport::Gzip 去解压,具体可以看http://api.rubyonrails.org/classes/ActiveSupport/Gzip.html#method-c-decompress

    如果用 Ruby 的话可以看看 Zlib::GzipReader,Eg:

    conn = Zlib::GzipReader.new(StringIO.new(some_gzipped_data))
    unzipped = conn.read
    conn.close
    
  • [问题] 部署的时候碰到的一些问题 at September 24, 2016

    #11 楼 @hesongGG 缺少 proxy_http_version 1.1,这个很关键

  • 如何快速优雅的在服务器上部署 Rails 程序 at September 10, 2016

    capistrano 3 之前会推荐 mina,现在只推荐 capistrano 3

  • 请教各位大神个问题, create 的时候怎么跳过 before_create 呢 at August 31, 2016

    需要注意的是 skip_callback 和 set_callback 不是 thread safe

    如果需要 thread safe 的话可以用https://github.com/partyearth/sneaky-save

  • Prev
  • 1
  • 2
  • 3
  • Next
关于 / RubyConf / Ruby 镜像 / RubyGems 镜像 / 活跃会员 / 组织 / API / 贡献者
由众多爱好者共同维护的 Ruby 中文社区,本站使用 Homeland 构建,并采用 Docker 部署。
服务器由 赞助 CDN 由 赞助
iOS 客户端 / Android 客户端 简体中文 / English