I have the following ActiveRecord call:
@payment = account.business.payments.find(params[:id])
Which searches through associations for a payment
with id of params[:id]
. However, this throws a RecordNotFound exception.
I'd like to call exists?
instead to see if the record exists to avoid throwing an exception. Doing Payment.exists?(account.business.payments.find(params[:id]))
does not work.
I'd like to search only the payments
that belong_to
that business
, and not all payments by doing Payment.exists?(:id => params[:id])
. That is so I can know that it's that particular account's business's payment.
How can I do that?
Note:
account
has_one business
and business
has_many payments
.
Use where
instead of find
, it will return an ActiveRecord::Relation
representing 0 or more records, off of which you can chain .exists?
:
@payments = account.business.payments.where(id: params[:id])
if @payments.exists?
# ...
end