视图中的多态关联

发布于 2024-10-19 13:15:41 字数 1484 浏览 4 评论 0原文

如何在我的视图中显示我的多态关联?我有以下模型。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伏妖词 2024-10-26 13:15:41

设计上存在缺陷。考虑以下情况:

user = User.create
cat = user.categories.create(:name=>"Education")

现在有一个类别:

> cat
# Category id: 1, name: "Education" categorizable_id:1 categorizable_type: "User"

当您添加博客并为其分配相同的类别时:

user.blogs.create(:category=>cat)

它会覆盖多态类型和 id:

# UPDATE "categories" SET "categorizable_type" = 'Blog', "categorizable_id" = 1,
  "updated_at" = '2011-02-27 06:20:29.968336' WHERE ("categories"."id" = 1)

现在该类别不再与用户关联:

user.reload
user.categories # => []

您确实在尝试建模这里是多对多的关系。我建议为 UserCategory 和 BlogCategory 添加联接表,并摆脱多态性,这没有帮助。

There is a flaw in the design. Consider the following:

user = User.create
cat = user.categories.create(:name=>"Education")

Now there is one category:

> cat
# Category id: 1, name: "Education" categorizable_id:1 categorizable_type: "User"

When you add a blog and assign the same category to it:

user.blogs.create(:category=>cat)

It overwrites the polymorphic type and id:

# UPDATE "categories" SET "categorizable_type" = 'Blog', "categorizable_id" = 1,
  "updated_at" = '2011-02-27 06:20:29.968336' WHERE ("categories"."id" = 1)

And now the category is no longer associated with the user:

user.reload
user.categories # => []

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.

情独悲 2024-10-26 13:15:41

请原谅我之前的回答。 zetetic的回答绝对有道理。尝试避免多晶型。

Pardon me for my earlier answer. zetetic's answer makes absolute sense. Try avoiding polymorphs.

口干舌燥 2024-10-26 13:15:41

更新:我决定多态模型不是可行的方法,并且我已经用很好的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文