I have a product, user and product_click model.
In product_click, I have column count
where I'm keeping a record, which user click which product how many times.
I have also tried
Product.sort_by{ |r| r.product_clicks.where(user_id: user.id).first.try(:count).to_i
which giving desire result but creating lots of query on running.
product_click.rb
belongs_to :product
belongs_to :user
product.rb
has_many :product_clicks, dependent: :destroy
user.rb
has_many :product_clicks, dependent: :destroy
Schema
create_table "product_clicks", force: :cascade do |t|
t.bigint "product_id"
t.bigint "user_id"
t.bigint "count", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
In controller
Product.left_outer_joins(:product_clicks).order("count DESC NULLS LAST")
Now i am looking something like this to work.
But here i can't able to check whether the count was of which particular user. If i'm adding
Product.left_outer_joins(:product_clicks).where(resource_clicks: { user_id: user.id }).order("count DESC NULLS LAST")
Then it showing a product which user clicked but not the 'nil' ones(which i never clicked).
I also tried this
Product.left_outer_joins(:product_clicks).where(product_clicks: { user_id: [user.id, nil] }).order("count DESC NULLS LAST")
but giving me all i clicked and all which no one else clicked. If someone click which i didn't then it not displaying to me.
product_clicks
? If it's a join table, consider to addhas_many through
relation. What'sresource_clicks
? – Excitor