Rails activity from polymorphism after

I keep track of the following tables:

Story (id, user_id, content)
Vote  (id, user_id, story_id)
Flag  (id, user_id, story_id)
etc..

with action table:

Activity (id, user_id,action, trackable_id, trackable_type)

Relations table:

Relationship (id, follower_id, followed_id)

I am currently receiving actions from users that follow as follows:

def get_activity_from_followers(current_user)
     followed_user_ids = "SELECT followed_id FROM relationships
                         WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids})",
          user_id: user.id)
end

My question is: how do I get the actions in which you have a tracked table (for example, history, voice, flag).
So right now I'm getting things like:

  • "someone you follow" posted a story
  • "someone you follow" voted for the story


I also want to get things like:

  • "someone you don't follow" voted for your story.
  • "someone you don't follow" is tagged with your story

How can I do it? Thank.

+4
3

, Activity, . - :

def get_votes(current_user)
 stories = current_user.stories
 votes = Votes.where(story_id: stories.map {|s| s.id})
 activity = Activity.where(action: 'vote', trackable_id: votes.map{|v| v.id})
end

. -, user.stories, , Action Activity "action" , trackback_id Vote Flag. , .

,

0

:

# User 
class User  :trackable
end

# Flag  (id, user_id, story_id)
class Flag  :trackable
end

# with an activity table:
# Activity (id, user_id,action, trackable_id, trackable_type)
class Activity  true
end

# The relationship table:
# Relationship (id, follower_id, following_id)
class Relationship  "User"
  belongs_to :following, :class_name => "User"
end

, , :

# List activities of my followings
Activity.where(:user_id => current_user.followings)

current_user.activities
0

Activity STI?

class Story < ActiveRecord::Base
  belongs_to :user
end

class Activity < ActiveRecord::Base
  belongs_to :story
  belongs_to :user
end

class Vote < Activity
  def to_partial_path
    'users/vote'
  end
end

class Flag < Activity
  def to_partial_path
    'users/flag'
  end
end

:

Activity.where(user_id: uid).all 

, ( , - :

render Activity.where(user_id: uid).all  
0

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


All Articles