[英]Is there is any way to write this Rails order query with Ruby symbols instead of strings?
在訂購記錄之前,我需要將一個表與另一個表連接起來。 到目前為止,我發現我只能使用字符串進行排序,如下所示:
customer.joins(:product).order("products.position")
我不喜歡使用硬編碼的字符串值,感覺不安全。 如果我可以改用Ruby符號,如下所示:
customer.joins(:product).order(:products => :position) #doesn't work
我該怎么做?
我有一種可能,因為您可以在連接表和執行where
時使用Ruby符號,如以下示例所示:
customer.joins(:product).where(:products => {:manufacturer_id => @manufacturer.id})
不,activerecord不是用這種方式設計的。 我能想到的最好原因是,您將如何用這種命名方式指定ASC
或DESC
? 您必須以字符串格式指定完整的語句才能使其正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.