0

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">
P0lska
  • 461
  • 1
  • 6
  • 17

2 Answers2

0

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 :)

  • Thanks for the reply, but I just renamed everything and it's not making any difference. Also, the word digest is used in the Michael Hartl tutorial in almost exactly the same way. Only difference that I can see is that I'm trying to use it on a model that belongs_to a user, not the user model itself. – P0lska Dec 09 '15 at 06:49
  • I changed the variable name from distribution_digest to signaturekey_hash and had the same problem. Then changed it to ekeycode and it worked. So it was the variable name after all... How can i avoid this kind of thing in future, it looks like I bumped into 2 variable names that I shouldn't use my accident, but this looks like it will be a constant source of headaches! – P0lska Dec 09 '15 at 08:08
  • I try not to use reserved words - here you have a list with all of them - http://www.rubymagic.org/posts/ruby-and-rails-reserved-words – Marcin Kornek Dec 09 '15 at 15:43
0

I got this working by removing :distribution_digest from attr_accessor

attr_accessor is a ruby code to (quickly) create setter and getter methods in a Class. That's all.

Now, what is missing as an explanation is that when you create somehow a link between a (Rails) model with a database table, you NEVER, NEVER, NEVER need attr_accessor in your model to create setters and getters in order to be able to modify your table's records.

https://stackoverflow.com/a/12938809

Community
  • 1
  • 1