这是为了什么?

我使用下面的风格,但不确定它是否是首选,因为如果我在结尾处漏掉了一个点,我可能会遇到很多问题而没有意识到这一点.

query = reservations_scope.for_company(current_company).joins{property.development}.
  group{property.development.id}.
  group{property.development.name}.
  group{property.number}.
  group{created_at}.
  group{price}.
  group{reservation_path}.
  group{company_id}.
  group{user_id}.
  group{fee_paid_date}.
  group{contract_exchanged_date}.
  group{deposit_paid_date}.
  group{cancelled_date}.
  select_with_reserving_agent_name_for(current_company, [
                                       "developments.id as dev_id",
                                       "developments.name as dev_name",
                                       "properties.number as prop_number",
                                       "reservations.created_at",
                                       "reservations.price",
                                       "reservations.fee_paid_date",
                                       "reservations.contract_exchanged_date",
                                       "reservations.deposit_paid_date",
                                       "reservations.cancelled_date"
  ]).reorder("developments.name")
query.to_a # ....

那么,chaining种方法与multiple lines种方法相比有哪些约定?我应该 Select 哪一种?

NOTE:我找不到Ruby coding style guide人中的好例子.

推荐答案

事实上,在《Ruby style guide》中有一节是关于这一点的:

采用一致的多行方法链接样式.有两个

  • (Option A)在上继续链式方法调用时

    # bad - need to consult first line to understand second line
    one.two.three.
      four
    
    # good - it's immediately clear what's going on the second line
    one.two.three
      .four
    
  • (Option B)在另一行继续链式方法调用时,

    # bad - need to read ahead to the second line to know that the chain continues
    one.two.three
      .four
    
    # good - it's immediately clear that the expression continues beyond the first line
    one.two.three.
      four
    

关于这两种替代风格的优点的讨论可以找到

Ruby-on-rails相关问答推荐

带有rails 7.1的Lightbox2未初始化

无法安装Ruby错误运行';__rvm_make-J4';,

Ruby on rails Admin-有没有一种方法可以在成对的管理控制器类中合并、重用应用程序控制器类?

在 Rails 7 中,可以将导入映射与 js Bundle 解决方案一起使用,还是它们是专有的?

Rails - 如何使用子对象的嵌套属性和强参数填充父对象ID?

Rails 中的转义值(类似于 mysql_real_escape_string())

撬Ruby 如何重新加载?

Rails 是否带有未授权异常?

在 Rails 的 ActiveRecord 中,touch 有什么用?

如何从 Rails 路由中删除控制器名称?

价格字段的字符串、小数或浮点数据类型?

Rails ActiveRecord 查询日期范围

如何使用失败try 次数使设计可锁定

Rails 文章助手 - a 或 an

有没有办法列出所有 belongs_to 关联?

Rspec - Rails - 如何遵循重定向

通过get in rails传递参数数组

在 Rails 中,在特定时区创建特定时间(不是现在)的最佳方式是什么?

Ruby on Rails:如何在 select_tag 中使用默认占位符?

Date.current 和 Date.today 有什么区别?