视图中的多态关联
如何在我的视图中显示我的多态关联?我有以下模型。
class Blog < ActiveRecord::Base
belongs_to :users
has_one :category, :as => :categorizable
end
class Category < ActiveRecord::Base
belongs_to :categorizable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :blogs
has_many :categories, :as => :categorizable
end
然而,我的问题是,当我尝试显示类别时,我得到了一个零。
rails console
=> user = User.first
=> user.blogs
[]
=> user.categories
[]
=> category = user.categories
=> category = Category.new
=> category.name = "Education"
=> category.save!
true
=> user.categories
[#<Category id: 1, name: "Education", categorizable_id: 1, categorizable_type: "User", created_at: "...", updated_at: "...">]
=> user.blogs.create(:title => "...", :body => "...", :category => category)
[#<Blog id: 1, user_id: 1, title: "...", body: "...", created_at: "...", updated_at: "...">]
=> blogs = user.blogs.all
=> blogs.each do |blog|
puts blog.category
end
nil
=> blogs.first.category
[#<Category id: 1, name: "Education", categorizable_id: 1, categorizable_type: "User", created_at: "...", updated_at: "...">]
我不明白,为什么当我使用each块时blog.category返回nil?如何通过我的视图显示多态模型的条目?
更新:
设计是,作为用户,我希望能够创建类别,并将它们分配给我的博客。每个博客都有一个类别,我希望通过类别模型将其作为可分类博客进行访问。
如果它按照逻辑试图说的那样按原样工作,那应该非常好,但目前还没有。多态模型应该是优雅的,但现在,多态对我来说很糟糕并且无法使用。我仍在等待有人帮助我提供更好的解决方案。
How do I display my polymorphic associations in my view? I have the following model.
class Blog < ActiveRecord::Base
belongs_to :users
has_one :category, :as => :categorizable
end
class Category < ActiveRecord::Base
belongs_to :categorizable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :blogs
has_many :categories, :as => :categorizable
end
However, my problem is, I get a nil when trying to display the category.
rails console
=> user = User.first
=> user.blogs
[]
=> user.categories
[]
=> category = user.categories
=> category = Category.new
=> category.name = "Education"
=> category.save!
true
=> user.categories
[#<Category id: 1, name: "Education", categorizable_id: 1, categorizable_type: "User", created_at: "...", updated_at: "...">]
=> user.blogs.create(:title => "...", :body => "...", :category => category)
[#<Blog id: 1, user_id: 1, title: "...", body: "...", created_at: "...", updated_at: "...">]
=> blogs = user.blogs.all
=> blogs.each do |blog|
puts blog.category
end
nil
=> blogs.first.category
[#<Category id: 1, name: "Education", categorizable_id: 1, categorizable_type: "User", created_at: "...", updated_at: "...">]
I don't get it, why blog.category is returning nil when I use the each block? How do I display the entries of my polymorphic model through my views?
Update:
The design is, as a user, I want to be able to create categories, and assign them to my blogs. Each blogs has one category on them, which I'd like to access via the Category model as a categorizable blog.
It should be really nice, if it's working as-is as the logic is trying to say, but currently it does not. Polymorphic models are supposed to be elegant, but right now, polymorphic sucks for me and unusable. I'm still waiting for someone to help me provide with a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设计上存在缺陷。考虑以下情况:
现在有一个类别:
当您添加博客并为其分配相同的类别时:
它会覆盖多态类型和 id:
现在该类别不再与用户关联:
您确实在尝试建模这里是多对多的关系。我建议为 UserCategory 和 BlogCategory 添加联接表,并摆脱多态性,这没有帮助。
There is a flaw in the design. Consider the following:
Now there is one category:
When you add a blog and assign the same category to it:
It overwrites the polymorphic type and id:
And now the category is no longer associated with the user:
You're really trying to model a many to many relationship here. I'd suggest adding join tables for UserCategory and BlogCategory, and getting rid of the polymorphism, which isn't helping.
请原谅我之前的回答。 zetetic的回答绝对有道理。尝试避免多晶型。
Pardon me for my earlier answer. zetetic's answer makes absolute sense. Try avoiding polymorphs.
更新:我决定多态模型不是可行的方法,并且我已经用很好的 ol' STI 解决了它,它做了同样的事情。
Update: I've decided that polymorphic model was not the way to go, and I've solved it with good ol' STI which does the same thing.