Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
# пользователь
class Person < ActiveRecord::Base
end
# продавец
class Seller < Person
# тут все те же методы, что и в вашем примере
end
seller = Seller.find(1)
seller.items
seller.first_name
def show
@articles = Author.find(params[:id]).articles.size # раз запрос
@items = Seller.find(params[:id]).items.size # два запрос
@buys = Buyer.find(params[:id]).purchased_items.size # три запрос
end
def show
@person = Person.find(params[:id]) # раз запрос
@articles = @person.becomes(Author).articles.size
@items = @person.becomes(Seller).items.size
@buys = @person.becomes(Buyer).purchased_items.size
end
def show
@person = Person.find(params[:id]) # раз запрос
@articles = Author.new(@person).articles.size
@items = Seller.new(@person).items.size
@buys = Buyer.new(@person).purchased_items.size
end
class Person < ActiveRecord::Base
# ...
has_many :purchased_items # купленные вещи
has_many :purchased_transactions # оплата покупок
end
class Seller < DelegateClass(Person)
...
end
person = Person.find(1)
seller = Seller.new(person)
class Person
concern 'person/selling'
end
class Person::Selling < Concern
def looking_good?
sold_items > 5 # sold items is a column on person
end
end
Person.first.selling.looking_good?
Я влюбился в DelegateClass