【发布时间】:2015-04-09 05:14:44
【问题描述】:
我正在使用简单表单和 Invoicing gem,但是当我尝试呈现表单时,我得到“InvoicingLedgerItems#new 中的 RuntimeError”和“Association :sender_id not found”。我想将 Users 表(Devise gem)的主键保存在 sender_id 字段中(因为用户是发票的发件人)。我尝试在模型中使用 foreign_key 选项,但似乎没有任何效果。这是我的代码,没有使用 foreign_key 选项。
型号:
class InvoicingLedgerItem < ActiveRecord::Base
acts_as_ledger_item
belongs_to :user
has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id
accepts_nested_attributes_for :line_items
end
class User < ActiveRecord::Base
has_many :invoicing_ledger_items
end
查看:
<%= simple_form_for @invoicing_ledger_item do |f| %>
<%= f.association :sender_id %>
<%= f.button :submit %>
<% end %>
架构:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
end
create_table "invoicing_ledger_items", force: true do |t|
t.integer "sender_id"
t.integer "recipient_id"
t.string "type"
t.datetime "issue_date"
t.string "currency", limit: 3, null: false
t.decimal "total_amount", precision: 20, scale: 4
t.decimal "tax_amount", precision: 20, scale: 4
t.string "status", limit: 20
t.string "identifier", limit: 50
t.string "description"
t.datetime "period_start"
t.datetime "period_end"
t.string "uuid", limit: 40
t.datetime "due_date"
t.datetime "created_at"
t.datetime "updated_at"
end
【问题讨论】:
-
InvoicingLedgerItem和Sender是什么关系? -
InvoicingLedgerItem 和 Sender 之间没有关系,因为没有 Sender 模型。发件人是用户,所以是 InvoicingLedgerItem 和用户之间的关系。
标签: ruby-on-rails ruby-on-rails-4 devise associations simple-form