在Rails中查询sqlite哈希列

时间:2016-01-16 20:31:09

标签: sql ruby-on-rails ruby-on-rails-4 activerecord sqlite

我在表中保存为散列的列,我正在尝试使用sql查询列以查找记录。到目前为止,我可以使用我找到的查询返回所有记录。那么,如何对其进行扩展,以便能够通过id provider找到包含参数的记录?另外,有没有关于如何在rails中使用sql查询的好教程?

<PublicActivity::Activity id: 233, trackable_id: 2, trackable_type: "User",  owner_id: 2, 
owner_type: "User", key: "like", parameters: {:id=>"27", :provider=>nil}, 
recipient_id: 3, recipient_type: "User", created_at: "2016-01-16 18:26:27", 
updated_at: "2016-01-16 18:26:27", read: false, user_recipients: nil>

这是查询。

PublicActivity::Activity.where("parameters LIKE ?", "%id%")

另外,当我尝试这个

PublicActivity::Activity.where("parameters.id LIKE ?", "%27%")

我收到此错误

SQLite3::SQLException: no such column: parameters.id: SELECT "activities".* FROM "activities"  WHERE (parameters.id LIKE '%27%')

1 个答案:

答案 0 :(得分:1)

怎么样?:

Activity.all.parameters.where('id like ? AND provider like ?', '%#{id}%', '%#{provider}%')