In my Ruby on Rails 4 app, I have a jsonb column called 'deals_usage'.
I would like to set up the following :
-
on create
deals_usage= { }
-
when a user loads deal number2 view (as an example), the attribute value change the value of deal_usage for the current user
deals_usage= { { Deal 2 = { clicks_performed_by_user = 0, purchases_performed_by_user= 0 } }
-
then if a user click something inside the Deal, the attribute value change to:
deals_usage= { { Deal 2 = {clicks_performed_by_user = 1, purchases_performed_by_user= 0 } }
So if a user participated in deal 2 with 1 click and in Deal n°5 with 1 purchase the deal_usage would be.
deals_usage={
{ Deal 2 = { clicks_performed_by_user = 1, purchases_performed_by_user= 0},
Deal 5 = {clicks_performed_by_user = 0, purchases_performed_by_user= 1}
}
Note: increments can only be +1 . If a user clicks: click+=1, if user purchase, purchase+=1. On this patter, it's quite simple.
Today I fail to achieve that. My current code below only does this:
-
on create
deals_usage= { }
-
when a user loads deal number2 view (as an example), it changes the value of deal_usage for the current user
deal_usage= { { clicks_performed_by_user = 0, purchases_performed_by_user= 0} }
And then I manage on each click and purchase to increment their values so that if user clicks in ANY DEAL, I get
deal_usage= { { clicks_performed_by_user = 1, purchases_performed_by_user= 0} }
BUT as you see I don't know on which Deal the click was performed. I don't know how to define in active record and save inside the database when clicks and purchases happen, when it comes to doing it PER DEAL.
I think one of the challenge involve creating a sort of dynamic store accessors as the number and the id of deals vary, each user will participate in different deals, and so on...
Here is my current code
Deals_controller.rb
class DealsController < ApplicationController
def show_deal_page
@deal = Deal.friendly.find params[:id]
#pass rails variables to js views with gon
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: @deal }
end
end
def click_inside_deal
@deal = Deal.friendly.find params[:id]
current_user.increment_per_deal_clicks_performed_by_user
respond_to do |format|
format.js
end
end
end
purchase click_inside_deal
@deal = Deal.friendly.find params[:id]
current_user.increment_per_deal_purchases_performed_by_user
respond_to do |format|
format.js { render :template => "deals/show_purchase_modal.js.erb" }
end
end
end
models/user.rb
class User < ActiveRecord::Base
rolify
# postgresql JSON attributes accessors
store_accessor :deals_usage,
# how to put here that the 2 following accessors are for EACH deal the user participate in ?
:clicks_performed_by_user,
:purchases_performed_by_user
def increment_per_deal_clicks_performed_by_user
self.update_attribute(:clicks_performed_by_user, self.clicks_performed_by_user += 1)
end
def increment_per_deal_purchases_performed_by_user
self.update_attribute(:purchases_performed_by_user, self.purchases_performed_by_user += 1)
end
def set_default_clicks_performed_by_user
self.clicks_performed_by_user ||= 0
end
def set_default_purchases_performed_by_user
self.purchases_performed_by_user ||= 0
end
Aucun commentaire:
Enregistrer un commentaire