ruby-on-rails-4strong-parametersbefore-save

Rails 4 before_create method not saving to DB


I'm trying to create a new hashed token to an SQL DB every time a Post request is sent to a URLwww.example.com/emailsig.json. I'm using before_create to run a function, which is being called but the variables that are created in the function aren't being saved to the DB. I'm new to rails and don't really know where to start trouble shooting this. I think it's got to do with strong parameters maybe?

Rails Model

class Distribution < ActiveRecord::Base
  belongs_to :user
  #validates :distribution_digest, presence: true
  attr_accessor :distribution_token, :distribution_digest
  before_create :create_distribution_digest
  validates :signature_id, presence: true

  def Distribution.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def Distribution.new_token
    SecureRandom.urlsafe_base64
  end

  private

    # Creates and assigns the distribution token and digest.
    # Functions create tokens and hashed tokens as expected.
    def create_distribution_digest
      self.distribution_token  = Distribution.new_token
      self.distribution_digest = Distribution.digest(distribution_token)
    end
end

Rails Controller

class DistributionsController < ApplicationController
  before_action :logged_in_user, only: [:email_sig]

  def create
    if current_user.microposts.find(params[:signature_id])
      @distribution = current_user.distributions.build(distribution_params)
      @distribution.save
      respond_with current_user.microposts.find(params[:signature_id])
    end
  end

  private

    def distribution_params
      params.require(:distribution).permit(:signature_id).merge(:distributed_at => Time.zone.now)
    end

end

Server Logs

Started POST "/emailSig.json" for 180.181.247.76 at 2015-12-09 05:01:47 +0000
Processing by DistributionsController#create as JSON
  Parameters: {"signature_id"=>8, "distribution"=>{"signature_id"=>8}}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Micropost Load (3.2ms)  SELECT  "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = ?  ORDER BY "microposts"."created_at" DESC LIMIT 1  [["user_id", 2], ["id", 8]]
   (0.2ms)  begin transaction
blah
pW00Dn7L9p7CnC6BysvImQ
  SQL (1.0ms)  INSERT INTO "distributions" ("signature_id", "distributed_at", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["signature_id", "8"], ["distributed_at", "2015-12-09 05:01:48.469044"], ["user_id", 2], ["created_at", "2015-12-09 05:01:48.484218"], ["updated_at", "2015-12-09 05:01:48.484218"]]
   (11.8ms)  commit transaction

Console Query (showing distribution_digest as nil)

  Distribution.last
  Distribution Load (0.4ms)  SELECT  "distributions".* FROM "distributions"  ORDER BY "distributions"."id" DESC LIMIT 1
=> #<Distribution id: 27, user_id: 2, signature_id: "8", distribution_digest: nil, distributed_at: "2015-12-09 05:01:48", created_at: "2015-12-09 05:01:48", updated_at: "2015-12-09 05:01:48">

Solution

  • It seems that reserved word digest in column name causes this problem. I tried to rename this column to sth_else and everything was working correctly, but with distribution_digest I can't even add this attribute in rails console. Try to rename this column and it should start working :)