2011-09-09 11 views
35

पठनीयता में सुधार के लिए हम निम्नलिखित कथन कैसे लिख सकते हैं?रूबी कोड सौंदर्यीकरण, एकाधिक लाइनों पर लंबे निर्देश विभाजित

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) 

निम्नलिखित संकलन नहीं है

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]) 

उत्तर

44

इसे इस तरह कार्य करें:

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) 
14

यह 1.9 में संकलन करना चाहिए। पिछले संस्करणों में यह वास्तव में अमान्य था।

48

भी संभव

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) 
+0

'\' बात बहुत उपयोगी है करने के लिए! धन्यवाद –

संबंधित मुद्दे