帮助在模型中建立多态关系
模型:
class Thread < ActiveRecord::Base
has_many :threaded, :through => :threaded, :foreign_key => :thread_id
class ThreadFeed < ActiveRecord::Base
belongs_to :threaded, :polymorphic => true
模型字段
Thread (id)
ThreadFeed (id, thread_id, threaded_id, threaded_type)
问题在于:
@thread.threaded
Rails 使用 (threaded_id, threaded_type) 作为外键,而我希望 thread_id 作为外键。
Models:
class Thread < ActiveRecord::Base
has_many :threaded, :through => :threaded, :foreign_key => :thread_id
class ThreadFeed < ActiveRecord::Base
belongs_to :threaded, :polymorphic => true
Model Fields
Thread (id)
ThreadFeed (id, thread_id, threaded_id, threaded_type)
Problem is with:
@thread.threaded
Rails is using (threaded_id, threaded_type) as the foreign key and I want thread_id to be the foreign key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个 Railscast,多态
它会让你更好地了解多态是如何工作的。
我注意到的第一个问题是它应该通过 :threadfeed,而不是 :threaded
在 Railscast 中,他有:
Take a look into this Railscast, Polymorphism
It will give you a better insight into how Polymorphism works.
First issue I notice is that it should be through :threadfeed, not :threaded
In the Railscast, he has:
第一个问题是Thread不知道feed是什么:
第二个问题是多态的复杂性。这是一篇关于它的精彩文章: http://blog.hasmanythrough.com/2006/ 4/3/多态贯通
The first problem is that Thread doesn't know what feed is:
The second problem is the complexity of polymorphic. Here's a great article on it: http://blog.hasmanythrough.com/2006/4/3/polymorphic-through