D
D
Demigodd2019-05-13 11:45:58
Ruby on Rails
Demigodd, 2019-05-13 11:45:58

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

How can I write something like this?
# Пример нерабочего запроса.
Author.joins(:book).where("book.category_type":  "Fantastic").count

That is, how to get authors whose related books have a category_type equal to " Fantastic ".

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Alekseiev, 2019-05-13
@Demigodd

As far as I understand from the links, you can do this:
It is possible to make still through scopes in model.

B
Boris Korobkov, 2017-04-28
@Tokenchik

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 question

Ask a Question

731 491 924 answers to any question