Rails “find_all_by” vs “.where”

前端 未结 2 1355
广开言路
广开言路 2020-12-05 17:53

I have the following code:

def maturities
  InfoItem.find_all_by_work_order(self.work_order).map(&:maturity)
end

I was thinking about c

2条回答
  •  渐次进展
    2020-12-05 18:37

    I think the main advantage is being able to add additional criteria to where, find_all_by is limited to the field of the dynamic selector. If you only have one condition you are searching by then I think it is a wash, but when you start adding 3 or 4, dynamic finders can be ugly. Hashes are nice to look at, and you could pass a hash of conditions as a parameter if needed. Dynamic finders are cool, but I think where scales in a cleaner way and is more readable.

提交回复
热议问题