同一模型和simple_form上的多个belongs_to

时间:2013-12-18 00:17:21

标签: ruby-on-rails ruby ruby-on-rails-4

我有两个belongs_to关联到同一模型,然后在

下面

simple_form

= f.association :from_user
= f.association :to_user

chats表中,我有from_user_idto_user_id

用户模型

has_many :from_user_chats, :foreign_key => :from_user_id, :class_name => 'Chat'
  has_many :to_user_chats, :foreign_key => :to_user_id, :class_name => 'Chat'

聊天模型

class Chat < ActiveRecord::Base
    belongs_to :from_user, class_name: 'User'
    belongs_to :to_user, class_name: 'User'
    belongs_to :chattable, polymorphic: true
end

然而,这让我失误:

undefined method `from_user_id' for #<Chat:0xcf283e0>

有原因吗?

Bactrace:

block in #<Class:0xd660bf0>#_app_views_chats__form_html_haml__904465522_69002796
app/views/chats/_form.html.haml, line 5
#<Class:0xd660bf0>#_app_views_chats__form_html_haml__904465522_69002796
app/views/chats/_form.html.haml, line 1
#<Class:0xd660bf0>#_app_views_chats_new_html_haml___186355583_68493228
app/views/chats/new.html.haml, line 3

0 个答案:

没有答案