ruby-on-rails - 如何使用 public_activity gem (Rails) 中的参数值?

标签 ruby-on-rails gem public-activity

我正在构建一个 Rails 应用程序。在我的应用程序中,有用户可以“关注”的项目。当用户关注其中一个页面时,如果有人上传/创建文件夹/文件,他/她将获得更新。

下面是刚刚新建文件夹时的截图:

enter image description here

下面是我的文件夹 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

结束

如您所见,:project_id:project_name 是新建文件夹时 public_activity 的参数正在创建。

下面是这个参数值在保存后在数据库中的样子的截图:

enter image description here

问题:

所以我的问题是,如何在我的 activities_controller 中使用这个参数值?

现在是我的事件 Controller 的代码:

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

我不想使用"owner_id:",而是使用parameters 列中的"project_id" 值。那我该怎么做呢?

非常感谢您! :)

最佳答案

参数字段包含一个简单的 yaml 转储,因此不太容易有效地搜索。

一个简单的解决方案是使用 LIKE 运算符,例如

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

您可能要考虑添加 custom fields相反。

关于ruby-on-rails - 如何使用 public_activity gem (Rails) 中的参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621508/

相关文章:

ruby-on-rails - rails 与 Oauth : multiple providers

ruby-on-rails - 如何在rails中检查check_box?

ruby-on-rails - Rspec 模型测试 - 模型中的 current_user

ruby-on-rails - 如何在事件提要中显示个人资料照片?

ruby-on-rails - 将项目成对地分发到容器中 - Rails

ruby-on-rails - Rails 3.1 不重新加载更改的 View

ruby - 为什么我不能用 rails 4.0.0.rc1 (ruby 2.0) 安装 postgresql

ruby - 在 OS X Yosemite 上安装 compass

ruby-on-rails - 使用 Best In Place gem,如何允许就地编辑链接?

ruby-on-rails - 如何为多个关联进行预先加载?