Answer the question
In order to leave comments, you need to log in
How to call a model method in Where conditions?
Imagine I need to take.
The number of Authors who write fiction.
There are 3 tables Author, Book, Category.
And there are methods for certain models.
# Model Book
def category_type
self.category.type
end
# Model Category
def type
if self.name.present?
self.name
else
"Other"
end
end
# Пример нерабочего запроса.
Author.joins(:book).where("book.category_type": "Fantastic").count
Answer the question
In order to leave comments, you need to log in
As far as I understand from the links, you can do this:
It is possible to make still through scopes in model.
category - public, it can be directly accessed from outside the class.
_related is private, it cannot be directly accessed from outside the class.
You need to refer to the $gallery->image->filePath type, but first show the Gallery model code. Do you have both a property image and a relation image in it? Then there will be confusion.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question