M. 9 Takeaways - Evanto/qna GitHub Wiki
exists?
- проверка существования объекта (записи) в бд
При создании голосовалки проверить, голосовал ли уже юзер за этот объект, можно методомexists?
, например:
def has_vote_by?(user)
self.votes.exists?(user_id: user.id)
end
exists?
- один из проверочных методов ActiveRecord, который проверяет, существует ли объект в бд, возвращает true
или false
.
Links:
- Существование объектов
- Запросы Active Record
- empty?, blank?, any?, exists? methods of Ruby on Rails ActiveRecord
.reload
ensures that an object has latest data that is currently stored in database (reloads the attributes of an object (like @question) from the db.
There are times when the in-memory state of an object may be out of sync with it's corresponding values in the database. When this happens, you must call .reload
on the in-memory object in order to refresh the values from the db. It is important to understand In-memory vs Database. Whenever a query is executed, it loads the relevant data from the database into memory.
Links:
как суммировать голоса в числовой колонке value:
def rating
votes.sum(:value)
end
controller_name.classify.constantize
Links: