更新:これはすべて愚かなエラーが原因だった:以前は、ActiveRecordが作成するメソッドの1つと同じ名前のメソッドを定義していたこれは適切な行動を覆し、すべてを壊していた。私は数時間以上質問に答えたり閉じることはできません。これを調べた人には謝ります!Rails 3.1 has_many、:through => not working(結合されたモデルはnilを返す)
私のRails 3.1のアプリケーションでhas_many, :through =>
関係に腹立たしい問題があります。
私が見る限り、2つの類似した関係が同じであるため、怒っています。
これらの関係の所有者は、このようにそれらを宣言しています
has_many :user_skills, :dependent => :destroy
has_many :skills, :through => :user_skills
has_many :user_roles, :dependent => :destroy
has_many :roles, :through => :user_roles
has_many :conversation_users
has_many :conversations, :through => :conversation_users
(私は承知していますここでテーブルを結合するために、私は標準的な命名法に従っていない - 私は両方-複数の慣例について読み、名前、アルファベット
最初の2組の関係(スキルと役割)は正常に機能します。
最終的な関係(会話)は完全には機能しません。 user.conversation_users
は予想される配列を返しますが、user.conversations
はnil
を返します。空の配列ではありません。nil
。
ここでは愚かなことをやっているかもしれないので、以下のConversationUser
またはConversation
モデルで間違った点を発見できる人には、とても感謝しています。
conversation_user.rb
class ConversationUser < ActiveRecord::Base
belongs_to :user, :inverse_of => :conversation_users
belongs_to :conversation, :inverse_of => :conversation_users
validates_presence_of :user
validates_presence_of :conversation
end
conversation.rb
class Conversation < ActiveRecord::Base
has_many :messages, :dependent => :destroy
has_many :conversation_users, :dependent => :destroy
has_many :users, :through => :conversation_users
validates_presence_of :unique_id
end
(私はまた、これらはhas_and_belongs_to_many
上has_many, :through =>
を正当化するのに十分な、本当に複雑ではないことを認識していますが、追加機能を計画)結合モデルが必要です。