rails 一对多关联不在数据库上保存记录
rails one-to-many assocaition not saving record on db
shop.rb
class Shop < ActiveRecord::Base
has_many :earning_rules
end
earning_rules.rb
class EarningRule < ApplicationRecord
belongs_to :user
belongs_to :shop
scope :order_rule, lambda {
where(status: 'true').where(name: 'Order').where(shop_id: shop.id)
}
validates :name, presence: true
validates :point, presence: true
def set_shop
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
end
end
earning_rules_controller.rb
def create
@earning_rule = current_user.earning_rules.new(earning_rule_params)
@earning_rule.set_shop
respond_to do |format|
if @earning_rule.save
format.html { redirect_to root_path, notice: 'Earning rule was successfully created.' }
format.json { render :show, status: :created, location: @earning_rule }
else
format.html { render :new }
format.json { render json: @earning_rule.errors, status: :unprocessable_entity }
end
end
end
def earning_rule_params
params.require(:earning_rule).permit(:user_id, :shop_id, :name, :point, :status)
end
form.html.erb
<%= simple_form_for earning_rule do |f| %>
<%= f.input :name %>
<%= f.input :point %>
<%= f.check_box :status, as: :boolean, checked_value: true, unchecked_value: false %>
<%= f.button :submit, class: 'btn btn-info btn-sm float-right ml-auto' %>
<% end %>
日志
I, [2019-11-20T09:42:01.490569 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Processing by EarningRulesController#create as HTML
I, [2019-11-20T09:42:01.490699 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Parameters: {"authenticity_token"=>"/BrJVX8bilaCc7yw/Hs0ngLh8O0DixA5lePv8bbLR1vpOBZ2MxBlk+qq7BCcM2xEh/XU8UjF6rWXUmZ1HQMf1Q==", "earning_rule"=>{"name"=>"nn", "point"=>"5", "status"=>"0"}, "commit"=>"Create Earning rule"}
D, [2019-11-20T09:42:01.492421 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] Shop Load (0.4ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
D, [2019-11-20T09:42:01.493549 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 3], ["LIMIT", 1]]
I, [2019-11-20T09:42:01.499620 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendering earning_rules/new.html.erb within layouts/embedded_app
I, [2019-11-20T09:42:01.507452 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/_form.html.erb (Duration: 7.1ms | Allocations: 2097)
I, [2019-11-20T09:42:01.507994 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/new.html.erb within layouts/embedded_app (Duration: 8.2ms | Allocations: 2223)
I, [2019-11-20T09:42:01.508949 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered shared/_navbar.html.erb (Duration: 0.3ms | Allocations: 179)
I, [2019-11-20T09:42:01.509206 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered layouts/_flash_messages.html.erb (Duration: 0.1ms | Allocations: 87)
I, [2019-11-20T09:42:01.509606 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Completed 200 OK in 19ms (Views: 10.2ms | ActiveRecord: 0.6ms | Allocations: 4864)
我不知道我错过了什么。 shop_id 数据不在日志参数中,这在我添加商店协会之前有效。任何帮助将不胜感激。
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
我打赌current_shopify_domain没有定义,所以找不到店铺。另外,您的专栏可能名为shop_id?为 user_id 和 shop_id 的存在添加验证规则,以便 Rails 通知您缺少的内容。
如果这没有帮助,请尝试在控制台中创建 EarningRule 以缩小问题范围。
shop.rb
class Shop < ActiveRecord::Base
has_many :earning_rules
end
earning_rules.rb
class EarningRule < ApplicationRecord
belongs_to :user
belongs_to :shop
scope :order_rule, lambda {
where(status: 'true').where(name: 'Order').where(shop_id: shop.id)
}
validates :name, presence: true
validates :point, presence: true
def set_shop
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
end
end
earning_rules_controller.rb
def create
@earning_rule = current_user.earning_rules.new(earning_rule_params)
@earning_rule.set_shop
respond_to do |format|
if @earning_rule.save
format.html { redirect_to root_path, notice: 'Earning rule was successfully created.' }
format.json { render :show, status: :created, location: @earning_rule }
else
format.html { render :new }
format.json { render json: @earning_rule.errors, status: :unprocessable_entity }
end
end
end
def earning_rule_params
params.require(:earning_rule).permit(:user_id, :shop_id, :name, :point, :status)
end
form.html.erb
<%= simple_form_for earning_rule do |f| %>
<%= f.input :name %>
<%= f.input :point %>
<%= f.check_box :status, as: :boolean, checked_value: true, unchecked_value: false %>
<%= f.button :submit, class: 'btn btn-info btn-sm float-right ml-auto' %>
<% end %>
日志
I, [2019-11-20T09:42:01.490569 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Processing by EarningRulesController#create as HTML
I, [2019-11-20T09:42:01.490699 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Parameters: {"authenticity_token"=>"/BrJVX8bilaCc7yw/Hs0ngLh8O0DixA5lePv8bbLR1vpOBZ2MxBlk+qq7BCcM2xEh/XU8UjF6rWXUmZ1HQMf1Q==", "earning_rule"=>{"name"=>"nn", "point"=>"5", "status"=>"0"}, "commit"=>"Create Earning rule"}
D, [2019-11-20T09:42:01.492421 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] Shop Load (0.4ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
D, [2019-11-20T09:42:01.493549 #92642] DEBUG -- : [96fe8592-245a-4537-8771-69a8261859e9] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 3], ["LIMIT", 1]]
I, [2019-11-20T09:42:01.499620 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendering earning_rules/new.html.erb within layouts/embedded_app
I, [2019-11-20T09:42:01.507452 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/_form.html.erb (Duration: 7.1ms | Allocations: 2097)
I, [2019-11-20T09:42:01.507994 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered earning_rules/new.html.erb within layouts/embedded_app (Duration: 8.2ms | Allocations: 2223)
I, [2019-11-20T09:42:01.508949 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered shared/_navbar.html.erb (Duration: 0.3ms | Allocations: 179)
I, [2019-11-20T09:42:01.509206 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Rendered layouts/_flash_messages.html.erb (Duration: 0.1ms | Allocations: 87)
I, [2019-11-20T09:42:01.509606 #92642] INFO -- : [96fe8592-245a-4537-8771-69a8261859e9] Completed 200 OK in 19ms (Views: 10.2ms | ActiveRecord: 0.6ms | Allocations: 4864)
我不知道我错过了什么。 shop_id 数据不在日志参数中,这在我添加商店协会之前有效。任何帮助将不胜感激。
shop = Shop.find_by(shopify_domain: current_shopify_domain)
self.shop = shop.id
我打赌current_shopify_domain没有定义,所以找不到店铺。另外,您的专栏可能名为shop_id?为 user_id 和 shop_id 的存在添加验证规则,以便 Rails 通知您缺少的内容。
如果这没有帮助,请尝试在控制台中创建 EarningRule 以缩小问题范围。