Rails / Active Record find with join, limit and of

2019-09-08 18:23发布

问题:

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