Track user activity on the Rails website

Users will have a user profile, and he wants each user to know who viewed their profile. The only way I can do this is to do the INSERT database every time someone views the profile page (perhaps using the Ajax call at the end of the page). This sounds like a serious performance issue, especially if the site is generating any significant traffic.

Any ideas on how to achieve this in execution, or is it just a character tracking this kind of thing?

Thank.

+3
source share
2 answers
  • ajax ?
    ..

  • (: + )

  • , - db (cassandra, redis ..)

+4

MySQL, "INSERT INTO... ON DUPLICATE KEY", , . , + , + + .

. , , , ID- , 12-16 .

, :

def self.up
  create_table :user_profile_views do |t|
    t.integer :user_id
    t.integer :profile_user_id
    t.integer :count, :default => 0
  end

  add_index :user_profile_views, [ :user_id, :profile_user_id ], :unique => true
end

:

def self.record_view_profile!(user, profile)
  connection.execute(sanitize_sql([ "
    INSERT INTO user_profile_views (user_id, user_profile_id, count)
      VALUES (%d,%d,1)
      ON DUPLICATE KEY UPDATE
       count=count+1
  ", user.id, profile.id ])
end

, .

+3

Source: https://habr.com/ru/post/1727651/


All Articles