鉴于这些表结构和模型,我如何在Ad类中实现“流行”方法 - 我只想返回按其点击次数排序的广告。
require 'sequel'
db = Sequel.sqlite
db.create_table(:users){
primary_key :id
}
db.create_table(:ads){
primary_key :id
foreign_key :user_id, :users
}
db.create_table(:hits){
primary_key :id
}
db.create_join_table(ad_id: :ads, hit_id: :hits)
class User < Sequel::Model
one_to_many :ads
end
class Hit < Sequel::Model
end
class Ad < Sequel::Model
many_to_one :user
many_to_many :hits
def self.popular
#return ads with most hits first
end
end
User.create
Ad.create user: User[1]
Ad.create user: User[1]
Ad[1].add_hit Hit.create
Ad[2].add_hit Hit.create
Ad[2].add_hit Hit.create
Ad.popular