0

I want to insert values to activities table using public_activity gem, but am not able to store records to parameters column in correct format. How to achieve this??

ActiveRecord::Base.connection.execute("INSERT INTO `activities`(`trackable_id`, `trackable_type`, 
        `owner_id`, `owner_type`, `key`, `parameters`, `created_at`, `updated_at`) 
        VALUES ('#{event_resource.resource_id}', '#{event_resource.resource_type}', '#{event.user.id}',
        'User', '#{event.action_type}', ':description: '#{event.description}', '#{event.created_at}', '#{event.updated_at}')")
Mani David
  • 1,382
  • 1
  • 14
  • 30

1 Answers1

0

You really need to read how ActiveRecord works, this might not be exactly correct but it should give you an idea how queries are done in rails

Activity.create(
  trackable: event_resource,
  owner: event.user,
  key: event.action_type,
  parameters: { description: event.description }
  created_at: event.created_at,
  updated_at: event.updated_at
)

You'll probably need to convert the hash to a string format, a json maybe

{ description: event.description }.to_json
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • thanks for your suggestion but am importing the event_resource table records into activities table, for that i cant able to set trackable: event_resource.resource, its coming as nil, so am trying it as sql insert to achieve in this case cant set parameters hash – Mani David Apr 02 '15 at 08:55
  • still, you don't need to go as far as inserting the whole thing as string, you're loosing all the validation, call backs, and sensitization of the data, you can replace `trackable` with `trackable_type` and `trackable_id` in the answer I've provided, each with their corresponding data. – Mohammad AbuShady Apr 02 '15 at 08:57
  • tried like the this **PublicActivity::Activity.create(trackable_id: event_resource.resource_id, trackable_type: event_resource.resource_type, key: event.action_type, parameters: { description: event.description }, owner: event.user, created_at: event.created_at, )** but the result **#"Beach Cove L" }, created_at: "2015-04-02 09:04:28">** , trackable id,type is coming as nil also its not setting the created_at field – Mani David Apr 02 '15 at 09:09
  • double check your variables, maybe those variables are actually nils – Mohammad AbuShady Apr 02 '15 at 09:42