您将如何在Rails 3中建模不同的帖子类型?

How would you model different post types in Rails 3?

我被困住了。我正在尝试在Rails 3中创建一个应用程序,该应用程序允许用户创建帖子,该帖子可以是图像,照片或文本。我希望我的帖子索引显示所有三种类型的帖子,并创建一个"发布者",以允许用户创建一个图像,照片或文本帖子。我已经尝试了一切,包括STI和has_one关系,但是似乎没有一种以Rails优化的方式出现,这使我发疯!我必须缺少一些东西。请帮忙!


这听起来像是您想要具有很多帖子的帖子类型

您的类型表和型号

1
2
3
4
5
6
7
8
9
  Types Table
| id  |   type  |
| 1   |  image  |
| 2   |  photo  |
| 3   |  text   |

class Type < ActiveRecord::Base
    has_many :posts
end

然后发布帖子表和模型

1
2
3
4
5
6
7
Post Table
|  id  | name | content | type_id | user_id |

class Post < ActiveRecord::Base
  belongs_to :types
  belongs_to :users
end

因此,当您进行查询时,您可以说:
Type.include(:posts).find(1)


检查此

如何在Ruby on Rails中实现Active Record继承?

http://mediumexposure.com/multiple-table-inheritance-active-record/