/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/ruby/ruby-511-102-026/main.rb
27 строк
784 B
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
# Пример кода в стиле Rails, демонстрирующий ООП концепции class Post < ApplicationRecord belongs_to :author has_many :comments has_many :tags, through: :post_tags validates :title, presence: true, length: { minimum: 5 } validates :content, presence: true scope :published, -> { where(published: true) } scope :recent, -> { order(created_at: :desc) } def publish! update(published: true, published_at: Time.now) end def to_param "#{id}-#{title.parameterize}" end end # Использование полиморфизма в коллекциях posts = Post.published.recent.limit(10) posts.each do |post| puts "#{post.title} by #{post.author.name}" puts "Comments: #{post.comments.count}" end