Question

I am going over Public Activity Gem Railscast Vid. (http://railscasts.com/episodes/406-public-activity)

Everything works fine, but I would like to be able to find all given activities performed except those that contain a recipient_id that belongs to Current_User

I have a method working(activities) which allows me to find all activities performed by all users

And this second method(notifications) to find all activities where recipient_id equals Current_User

CONTROLLER

**(finds all activities)**
def activities
  @title = "Activities"
  @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.followed_users.all, owner_type: "User") 
  @user = User.find_by_username(params[:id])
  render 'activities'
end

**(find all notifications where recipient_id is Current_User)**
def notifications
  @title = "Notifications"
  @activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user, recipient_type: "User") 
  @user = User.find_by_username(params[:id])
  render 'notifications'
end

Is there a way to combine them so that the first method (activities) finds all activities EXCEPT those found in method 2(notifications) where a recipient_id equals current_user

I know this is pretty easy to do....i just cant figure it out. Please help....New to Rails :D

My current DB looks/stores everything like this

#<PublicActivity::Activity id: 108, trackable_id: 133, trackable_type: "Relationship",
owner_id: 1, owner_type: "User", key: "relationship.create", parameters: {}, 
recipient_id: 1, recipient_type: User, created_at: "2013-06-16 07:37:28", 
updated_at: "2013-06-16 07:37:28">
Was it helpful?

Solution

You would have to use a custom SQL query, but something like this should solve your problem:

@activities = PublicActivity::Activity.where("recipient_id != ?", current_user.id).where(recipient_type: "User", owner_id: current_user.followed_users.all, owner_type: "User")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top