Ayanami
Ayanami

Reputation: 85

Model not saving values to history model in Rails

I want to save to an history table (configs_histories) all the values inside the configs table everytime a user edits or creates a new Config. So I use the callback after_save inside my Config model to call the save_configs_map_to_history function. However, when I edit a config, rails fails to save the values in the history table, showing rollback transaction in the console. What am I doing wrong?

Please help, thanks in advance!

config.rb:

class Config < ApplicationRecord  

  after_save :save_configs_map_to_history

  belongs_to :mirth

  has_paper_trail

  def save_configs_map_to_history

    configs = Config.all

    version_no = (ConfigsHistory.maximum('version') || 0) + 1

    configs.each do |config|

      ConfigsHistory.create!(key: config.key, value: config.value,
                             comment: config.comment, mirth_id: config.mirth_id,
                             version: version_no, config_created_at: config.created_at,
                             config_updated_at: config.updated_at, config_created_by: config.created_by,
                             config_updated_by: config.updated_by)

    end

  end

end

part of my console log:

...
(0.0ms)  SELECT MAX("configs_histories"."version") FROM "configs_histories"
    Config Load (1.0ms)  SELECT "configs".* FROM "configs"
    Mirth Load (1.0ms)  SELECT  "mirths".* FROM "mirths" WHERE "mirths"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
    (27.0ms)  rollback transaction
    Rendering configs/_edit.html.erb within layouts/application
...

configs_history.rb:

class ConfigsHistory < ApplicationRecord  

 belongs_to :mirth
 belongs_to :user

end

Upvotes: 0

Views: 54

Answers (2)

Ayanami
Ayanami

Reputation: 85

I found the problem! It was missing a mandatory field (user_id) in the ConfigsHistory.create and it failed to save the value, but the error was not shown (need to find a way to output those kind of errors). Thanks for your time and suggestions!

Upvotes: 0

Aniket Rao
Aniket Rao

Reputation: 778

  1. Add gem 'pry-rails' to your gemfile and run bundle
  2. Restart your server

In your code

class Config < ApplicationRecord  

  after_save :save_configs_map_to_history

  belongs_to :mirth

  has_paper_trail

  def save_configs_map_to_history

    configs = Config.all

    version_no = (ConfigsHistory.maximum('version') || 0) + 1

    configs.each do |config|

    binding.pry
      config_history = ConfigsHistory.create!(key: config.key, value: config.value,
                             comment: config.comment, mirth_id: config.mirth_id,
                             version: version_no, config_created_at: config.created_at,
                             config_updated_at: config.updated_at, config_created_by: config.created_by,
                             config_updated_by: config.updated_by)

    end

  end

end

When you run your code check your rails console, it will halt and you will get a console to debug the problem.

In that window copy your next line and you will see it fail.

After that just do config_history.errors and see the errors messages.

That should give you enough info to debug your situation and resolve your errors.

To know how to use pry check this out https://www.youtube.com/watch?v=A494WFSi6HU

Upvotes: 2

Related Questions