Rails 3.1 has_many, :through =>;不工作(加入的模型返回零)
更新:这都是由于一个愚蠢的错误造成的:之前,我定义了一个与 ActiveRecord 创建的方法之一同名的方法,它掩盖了正确的行为并破坏了一切。我在几个小时内无法回答/关闭问题,向调查此问题的任何人致歉!
我的 Rails 3.1 应用程序中的 has_many, :through =>
关系存在令人恼火的问题。
这是令人愤怒的,因为据我所知,它与两种都有效的相似关系相同。
这些关系的所有者这样声明它们:(
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
我知道我在这里没有遵循连接表的标准命名法 - 我只在设置后阅读了双复数、名称字母顺序的约定,稍后我将重构)
前两对关系(技能和角色)运作良好。
最终的关系(对话)并没有完全发挥作用。 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_many, :through =>
优于 < code>has_and_belongs_to_many,但计划的附加功能将需要连接模型。)
Update: This was all due to a stupid error: previously, I had defined a method with the same name as one of the methods ActiveRecord creates, which was masking the proper behaviour and breaking everything. I can't answer/close the question for a few more hours, apologies to anyone who looked into this!
I have an infuriating problem with a has_many, :through =>
relationship in my Rails 3.1 app.
It is infuriating because as far as I can see it is identical to two similar relationships which both work.
The owner of these relationships declares them like this:
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
(I am aware I have not followed standard nomenclature for join tables here - I only read about the convention of both-plural, names-alphabetical after setting this up, and I will refactor later)
The first two pairs of relationships (skills and roles) work just fine.
The final relationship (conversations) does not work fully. user.conversation_users
returns the expected array, but user.conversations
returns nil
. Not an empty array, nil
.
I may well be doing something stupid here, so I would be very grateful to anyone who can spot something wrong with the ConversationUser
or Conversation
models below.
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
(I am also aware that these are not really complex enough to justify has_many, :through =>
over has_and_belongs_to_many
, but planned additional functionality will require join models.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答结束问题:
这都是由于一个愚蠢的错误造成的:之前,我定义了一个与 ActiveRecord 创建的方法之一同名的方法,它掩盖了正确的行为并破坏了一切。
Answering to close question:
This was all due to a stupid error: previously, I had defined a method with the same name as one of the methods ActiveRecord creates, which was masking the proper behaviour and breaking everything.