Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby code beautification, split long instructions on multiple lines

How can we write the following statement to improve readability?

Promotion.joins(:category).where(["lft>=? and rgt<=?", c.lft, c.rgt]).joins(:shops).where(:promotions_per_shops => { :shop_id => shops_id }).count('id', :distinct => true)

The following doesn't compile

Promotion.joins(:category)
         .where(["lft>=? and rgt<=?", c.lft, c.rgt])
         .joins(:shops)
         .where(:promotions_per_shops => { :shop_id => shops_id })
         .count('id', :distinct => true)

syntax error, unexpected '.', expecting kEND
                     .where(["lft>=? and rgt<=?", c.lft, c.rgt])
like image 832
EricLarch Avatar asked Sep 09 '11 09:09

EricLarch


2 Answers

Also possible to do

Promotion.joins(:category) \
         .where(["lft>=? and rgt<=?", c.lft, c.rgt]) \
         .joins(:shops) \
         .where(:promotions_per_shops => { :shop_id => shops_id }) \
         .count('id', :distinct => true)
like image 139
EricLarch Avatar answered Sep 29 '22 12:09

EricLarch


Do it like this:

Promotion.joins(:category).
         where(["lft>=? and rgt<=?", c.lft, c.rgt]).
         joins(:shops).
         where(:promotions_per_shops => { :shop_id => shops_id }).
         count('id', :distinct => true)
like image 31
Geo Avatar answered Sep 29 '22 10:09

Geo