Ruby China
  • 社区
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • 注册
  • 登录
@blueplanet
高级会员
第 2650 位会员 / 2012-06-25

20 篇帖子 / 226 条回帖
3 关注者
2 正在关注
16 收藏
GitHub Public Repos
  • hotwire-scaffold 2

  • bitcoin-ruby 1

  • blueplanet.github.com 1

  • re-work 0

  • twa-template 0

    Boilerplate starter template for a new Telegram Web App (TWA) interacting with the TON blockchain

  • bundler 0

  • account-abstraction 0

  • blueplanet 0

  • gatsby-starter-default 0

  • solidity 0

    Solidity, the Smart Contract Programming Language

More on GitHub
  • 概况
  • 话题
  • 回帖
  • 收藏
  • 正在关注
  • 关注者
  • 有使用 Slim 的吗?Atom/Sublime/Textmate syntax packages 求接手 at 2017年08月18日

    @fredwu 举手!

  • Go 写个小程序,替换掉 Sidekiq at 2017年05月16日

    @moliliang 我觉得对于你的问题来说,你的解决方法很新颖,而且很简洁,需要赞一个!

  • 夭寿了,关于单表继承和多态的使用 (STI and polymorphs) at 2016年12月21日

    @Qcoder 没注意已经这么多讨论了,画蛇添足了。。。

  • 夭寿了,关于单表继承和多态的使用 (STI and polymorphs) at 2016年12月21日

    @Qcoder 到底是不是 bug 还是有争议的。因为实际上你的代码是可以动的。 参考你给的连接尝试了一下,下面的测试是通过的

    分析

    最后的测试里面, assert_equal Bicycle.name, price.priceable.class.name 是重点。 price.priceable_type 确实是 Car ,但这只是从Price model方向去找priceable的时候的问题, 而实际上创建 price.priceable 这个对象的时候,到底是Car还是Bicycle,看的是cars这个表里的type字段的值。

    结论

    • 对于你的Bicycle级连拿prices的时候,不定义方法应该也是正常能够跑的
    • 从Price去找priceable的纪录的时候,应该用Car还是实际的Bicycle,这个属于 Rails 的设计问题,个人觉得可以选择性的忽略 😅
    require 'active_record'
    require 'minitest/autorun'
    
    ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
    
    ActiveRecord::Base.connection.instance_eval do
      create_table(:cars) do |t|
        t.string :name
        t.string :type
        t.string :bicycle_column1
        t.integer :prices_count
      end
    
      create_table(:prices) do |t|
        t.string :name
        t.string :priceable_type
        t.integer :priceable_id
      end
    end
    
    class Car < ActiveRecord::Base
       has_many :prices, :as => :priceable, :dependent => :destroy
    end
    
    class Bicycle < Car
      def aaa?
        bicycle_column1 == 'aaa'
      end
    end
    
    class Price < ActiveRecord::Base
      belongs_to :priceable, :polymorphic => true, :counter_cache => true
    end
    
    class MyTest < Minitest::Test
      def test_1
        bicycle1 = Bicycle.create name: 'bircycle1', bicycle_column1: 'aaa'
        bicycle1.prices.create name: 'price1'
    
        price = Bicycle.first.prices.first
    
        assert_equal Bicycle.name, price.priceable.class.name
        assert_equal true, price.priceable.aaa?
        assert_equal bicycle1.prices.first, Price.first
      end
    end
    
  • 使用 Rails 自带的 Form Builder 来重构你的 Form at 2016年12月17日

    @hfpp2012 嗯,也对。 如果这个定制只是针对一个项目的话,基本上 col-sm-10 之类的应该都是类似的,确实不需要 simple_form 之类的了。 👍

  • 使用 Rails 自带的 Form Builder 来重构你的 Form at 2016年12月14日

    先赞一个!!!

    其次,上面的代码里面的 required,直接用 new_record?的返回值就好了

    <%= f.image_file_field :image, required: @product.new_record?, class: 'form-control' %>
    

    第三是个疑问:自定义了这些方法之后,实际上在项目里面不同的页面上的布局还是不一样的。那样的话就需要把布局那部分的col-sm-10分离到方法的参数里面,而这样的情况应该还会有很多。那如果我们在项目中不断的增加需要的参数之后,最终的结果会不会还是变成了 simple_form 或者bootstrap_form这样的东西呢?

  • 为 module 使用 ActiveSupport::Concern 并 include 后,怎样马上使用 include 后的方法? at 2016年11月01日

    @bindiry 1 楼的回答是正确的方法。 如果你想定义类方法,把你的方法定义在ClassMethods这个 module 里面就可以了。

    module Writable
      extend ActiveSupport::Concern
    
      included do
        attr_reader :write_worker
      end
    
      module ClassMethods
        def set_options(worker)
          @worker = worker
        end
      end
    end
    
    
  • 《Ruby on Rails 教程 (原书第 4 版)》发布 at 2016年06月29日

    火速入手!

  • 发现一个神奇的 i18n 的管理工具 at 2016年06月23日

    @modacker 或许和你的需求有点出入,如果你有多个 App 需要管理 I18n 的话,可以尝试一下这个,thoughtbot 出品的。 http://railscasts.com/episodes/336-copycopter?language=en&view=asciicast

  • 关于在 Rails Model 中使用 Enum (枚举) 的若干总结 at 2016年01月09日

    补充几个踩过的坑

    • 定义enum时自动增加的各个值对应的?和!方法是model范围内有效的。如果不同的字段有同名的值的时候一定要注意
    # user.rb
      enum status: [:temporary, :active, :deleted]
      enum admin_status: [:active, :super_admin]
    
    # rails console
    irb(main):001:0> u = User.neww
    ArgumentError: You tried to define an enum named "admin_status" on the model "User", but this will generate a instance method "active?", which is already defined by another enum.
    ...
    
    • 修改以前定义好的enum时需要注意历史数据
    # 原来是这样的定义
    enum status: [:temporary, :active, :deleted]
    # 修改后,
    enum status: [:temporary, :active, :waiting, :deleted]
    # 如果这样修改的话,以前的`deleted`的数据修改后就变成`:waiting`了!!! :sleepy: 
    

    所以,不推荐使用[:temporary, :active, :deleted]的方式,尽量使用{ temporary: 1, active: 2, deleted: 3]这样的明确定义数值的方式

    • 自动增加的status=方法,参数是不允许的值的时候会抛出ArgumentError。在直接从网页上接收status的值的场景下一定要注意。 这种情况只有在运行的时候才会报错所以异常危险!!!
    irb(main):004:0> u1.status = 'aaa'
    ArgumentError: 'aaa' is not a valid status
        from /Users/blueplanet/sandbox/enum_test/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5/lib/active_record/enum.rb:104:in `block (3 levels) in enum'
        from (irb):4
    
    • 由于会自动增加scope,所以要注意不要覆盖掉 Rails 默认的scope。比如enum status: { none: 0, active: 1, deleted: 2}就会覆盖掉Rails4增加的nonescope 了。
  • 准备着手翻译出版《Rebuilding Rails》,有人愿意买吗? at 2015年12月24日

    必须买!也希望是电子版!

  • 字符串按每两个一组怎么分割 at 2015年08月21日

    "askxjdisiskx".chars.in_groups_of 2

    http://apidock.com/rails/Array/in_groups_of

  • 七牛融合 CDN 管理服务上线,携手 Ruby China 社区送券福利升级! at 2015年07月20日

    erguolinge at gmail.com

  • Ruby 基础 - RubyGem,如何开发一个自己的 Gem at 2015年07月02日

    @cicholgricenchos 你的问题属于 git 的问题吧。 在空文件夹里创建一个零字节的文件就成。 按惯例一般是.gitkeep

  • Rails 有什么好的生成 API Doc 的工具么 at 2015年06月24日

    https://github.com/r7kamura/autodoc 利用 RSpec 的 tag 功能

  • 类似 “按照最新发布文章时间排序的用户列表” 的优雅的实现方法 at 2015年06月16日

    @huacnlee 确实,这样的方式确实很优雅。

    原来是觉得没必要在用户表里面增加字段,现在仔细想想,在读取纪录次数远远大于更新纪录的次数的情况下,还是增加字段的方式执行效率高。

    非常感谢!

  • 如何更好的同步 github 上的数据 at 2014年12月31日

    @luffycn 除了第一次获取项目信息之外,其他的事件应该都可以用 github 的通知功能来实现。

  • Pomotodo for iOS at 2014年05月05日

    非常好的工具,支持一下!

  • GIRA, yet another github kanban board at 2014年04月16日

    @geogeo 你好。刚刚看了 gira,非常同一你的观点。Milestone 绝对应该用起来。 但我访问 http://getgira.github.io/ ,授权 github 的访问权限以后只是显示空白页面。要怎么访问我自己的 repo 呢?

  • 配合 iTerm2 实现快速 tmux 窗口切换小技巧 at 2014年04月01日

    很好用!非常感谢!

  • 又来发 Dimensions 的 Promotion Code 了 at 2014年03月11日

    T9W6MFYNYKNW を使った!有難うございます!

  • sandbox 下 user.save 能够保存 但是数据库中不显示 好奇妙的问题 at 2014年03月10日

    #6 楼 @sensi 这就是“事务”的特点。没有最后 commit 的话其他的数据库链接是看不到的。查一下 mysql 的事务看一下吧

  • sandbox 下 user.save 能够保存 但是数据库中不显示 好奇妙的问题 at 2014年03月09日

    sandbox 模式用的是数据库的事务的功能。 推出的时候会执行 rollback 取消所有的操作

  • Github 开发的心编辑器 Atom 邀请码 at 2014年02月28日

    #65 楼 @varro 已发送,请查收!

    我的 3 个也都送出去了!希望大家继续接力!

  • Github 开发的心编辑器 Atom 邀请码 at 2014年02月28日

    #57 楼 @greyby 邀请码已发,请查收! 可以的话也给大家发吧

  • Github 开发的心编辑器 Atom 邀请码 at 2014年02月27日

    #49 楼 @Peter 收到!非常感谢! 哪位还没拿到的? @ 联系我

  • Github 开发的心编辑器 Atom 邀请码 at 2014年02月27日

    #26 楼 @Peter 还有邀请码么?blueplanet42 gmail

  • Github 开发的心编辑器 Atom 邀请码 at 2014年02月27日

    blueplanet42 # gmail 谢谢!

  • LaunchRocket,更容易的启动由 Homebrew 安装的服务 at 2014年02月17日

    效果不错!

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