【问题标题】:Rails Simple Form Association Not Found未找到 Rails 简单表单关联
【发布时间】: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


【解决方案1】:

你需要通过外键'sender_id'与用户模型建立关系

型号

class InvoicingLedgerItem < ActiveRecord::Base  
  acts_as_ledger_item  
  belongs_to :user, foreign_key: :sender_id  
  has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id  
  accepts_nested_attributes_for :line_items  
end

需要与用户关联为隐藏字段

查看:

<%= simple_form_for @invoicing_ledger_item do |f| %>  
  <%= f.association :user, :as => :hidden, :input_html => { :value => curren_user.id } %>  
  <%= f.button :submit %>  
<% end %>

【讨论】:

  • 啊,我不明白我需要 f.association :user,而不是 f.association :sender_id。谢谢!
【解决方案2】:

您应该首先在架构中使用user_id 而不是sender_id。然后在InvoicingLedgerItem 模型中使用alias_attribute :user_id, :sender_id,通过此帮助方法您可以为字段名称分配新名称。

更多参考请访问http://api.rubyonrails.org/classes/Module.html

【讨论】:

  • 我想过改变架构,但是 InvoicingLedgerItem 将来可能会被子类化,并且发件人有时可能不是用户。 (例如,发件人可能是一家公司。)这就是我决定不更改架构的原因。
猜你喜欢
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 2011-02-06
相关资源
最近更新 更多