How to use parameters value from public_activity gem (Rails)?

╄→гoц情女王★ 提交于 2019-12-22 10:56:27

问题


I am building a Rails app. And in my app, there are Projects where users can "Follow". When a user follows one of the pages, he/she will get updates if somebody uploads/creates a folder/file.

Below is the screenshot when somebody just created a new folder:

And below is the code for "Create" action in my Folder controller:

 def create
 @folder = current_user.folders.where(project_id: params[:project_id]).create(folder_params)

 respond_to do |format|
  if @folder.save
    @folder.create_activity :create, owner: current_user, :params => {
       :project_id => proc {|controller, project| @folder.project.id},
       :project_name => proc {|controller, project| @folder.project.name},
      }

    format.html { redirect_to @folder.project, notice: 'Folder was successfully created.' }
    format.json { render :show, status: :ok, location: @folder }
  else
    format.html { render :new }
    format.json { render json: @folder.errors, status: :unprocessable_entity }
  end
end

end

As you can see :project_id and :project_name are the parameters for the public_activity when a new folder being created.

And below is the screenshot on how this parameters value looks like in the database after they were saved:

QUESTION:

So my question is, how do i use this parameters values in my activities_controller?

Here is the code for my activities controller right now:

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.following_users, owner_type: "User")
  end
end

Instead of using "owner_id:", I want to use the "project_id" value from parameters column. So how can i do this?

Thank you very much in advanced! :)


回答1:


The parameters field contains a simple yaml dump, so not really easy to search efficiently.

A simple solution would be to use LIKE operator, for instance

PublicActivity::Activity.where("parameters LIKE '%project_id: #{@project.id}%'")

You might want to consider to add custom fields instead.




回答2:


Thanks for the answer, but I got a better solution than using the parameters value or custom field. Here is how my activities_controller looks like right now:

class ActivitiesController < ApplicationController

  def index
    activity_table = PublicActivity::Activity.arel_table

    # We want to view all activity of folders related to projects we are follwing
    folder_ids = Folder.where(project_id: current_user.following_projects.pluck(:id)).pluck(:id)

    # Generate query for all activity related to folders we care about
    folders_query = activity_table[:trackable_type].eq('Folder').and(
         activity_table[:trackable_id].in(folder_ids)
    )

    # Generate query for all users that we follow
    users_query = activity_table[:owner_id].in(current_user.following_users.pluck(:id))

    activity_query = folders_query.or(users_query)
    @activities = PublicActivity::Activity.where(activity_query)
  end
end

By using this way, I could easily combine the activities from the "Users" and also from the "Projects" that the user follows.

You can modify it to add any other activities such as from the "Comments" or "Voting".

Hope this will help other people out there that are using public_activity gem! :)



来源:https://stackoverflow.com/questions/31621508/how-to-use-parameters-value-from-public-activity-gem-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!