Rails 4 before_create 方法未保存到数据库
Rails 4 before_create method not saving to DB
每次将 Post 请求发送到 URLwww.example.com/emailsig.[= 时,我都在尝试为 SQL 数据库创建一个新的散列令牌30=]。我正在使用 before_create 到 运行 函数,该函数正在被调用,但在函数中创建的变量未保存到数据库中。我是 rails 的新手,真的不知道从哪里开始解决这个问题。我认为这可能与强大的参数有关?
Rails 型号
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 控制器
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
服务器日志
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
控制台查询(显示 distribution_digest 为 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">
似乎是列名中的保留字 digest
导致了这个问题。我试图将此列重命名为 sth_else
并且一切正常,但是使用 distribution_digest
我什至无法在 rails 控制台中添加此属性。尝试重命名此列,它应该开始工作:)
我通过从 attr_accessor
中删除 :distribution_digest
来实现此功能
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.
每次将 Post 请求发送到 URLwww.example.com/emailsig.[= 时,我都在尝试为 SQL 数据库创建一个新的散列令牌30=]。我正在使用 before_create 到 运行 函数,该函数正在被调用,但在函数中创建的变量未保存到数据库中。我是 rails 的新手,真的不知道从哪里开始解决这个问题。我认为这可能与强大的参数有关?
Rails 型号
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 控制器
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
服务器日志
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
控制台查询(显示 distribution_digest 为 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">
似乎是列名中的保留字 digest
导致了这个问题。我试图将此列重命名为 sth_else
并且一切正常,但是使用 distribution_digest
我什至无法在 rails 控制台中添加此属性。尝试重命名此列,它应该开始工作:)
我通过从 attr_accessor
:distribution_digest
来实现此功能
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.