将查询从SQL转换为ActiveRecord

时间:2013-05-14 19:53:44

标签: ruby-on-rails rails-activerecord

如何使用ActiveRecord获得相同的结果?

SELECT categories.* FROM categories
INNER JOIN levels ON levels.id = categories.level_id where levels.description <= "Medium"

1 个答案:

答案 0 :(得分:1)

没有更多细节很难准确,但它应该是这样的:

Category.joins(:level).where('levels.description <= "Medium"')

我认为这也应该可以解决问题:

Category.joins(:level).where(Level.arel_table[:description].lteq('Medium'))