开发者

Polymorphic associations in views

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, 开发者_Go百科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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜