I have two ruby scope but they are not completely using activecord. How can I change it to use only activerecord or sql?
article.rb
class Article < Comfy::Cms::Page
has_many :view_mappings
has_many :favorite_mappings
scope :top_by_number_of_favorites, -> { article.sort_by(&:favorite_score).take(10) }
scope :top_by_number_of_views, -> { article.sort_by(&:view_score).take(10) }
def favorite_score
favorite_mappings.since(2.weeks.ago).count * 5
end
def view_score
view_mappings.since(2.weeks.ago).count
end
schema.rb
create_table "view_mappings", force: :cascade do |t|
t.integer "article_id"
t.date "date_viewed"
t.string "device_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "favorite_mappings", force: :cascade do |t|
t.integer "article_id"
t.string "device_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.date "date_liked"
end
Aucun commentaire:
Enregistrer un commentaire