Rails / Active Record find with join, limit and offset

本秂侑毒 提交于 2019-12-12 05:41:40

问题


I've put together the following query, which works as I expect it to:

  stuff = @thing.children.find(
    :all,
    :joins => :other,
    :conditions => {:others => {:another_id => some_id}},
    :limit => my_limit,
    :offset => my_offset,
  )

However, queries of the form find(:all) are deprecated. I have tried changing my query to the following form:

  stuff = @thing.children.find(
    :joins => :other,
    :conditions => {:others => {:another_id => some_id}},
    :limit => my_limit,
    :offset => my_offset,
  ).all

but this throws a database error. What's the correct way to write this query?


回答1:


Short of rewriting it to Arel, you can simply, change the .find to .all, and remove the :all symbol like so:

stuff = @thing.children.all(
  :joins => :other,
  :conditions => {:others => {:another_id => some_id}},
  :limit => my_limit,
  :offset => my_offset,
)


来源:https://stackoverflow.com/questions/10721787/rails-active-record-find-with-join-limit-and-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!