我有一个主题模型,它属于分类和级。
知道每个主题属于最大的5-6级和2-3个类别,是否可以使用数组将每个主题的类别it和级别it存储在2列中:true of Postgresql,或者这样做是不好的做法?
发布于 2015-12-14 23:00:34
has_and_belongs_to_many
的关系对你有用吗?
class Topic
has_and_belongs_to_many :levels
has_and_belongs_to_many :categories
end
class Category
has_and_belongs_to_many :topics
end
class Level
has_and_belongs_to_many :topics
end
create_table :categories_topics do |t|
t.integer :topic_id
t.integer :category_id
end
create_table :levels_topics do |t|
t.integer :level_id
t.integer :topic_id
end
这将使结构看起来像:
|--------| |--------------|
| Topics | ---> | LevelsTopics |
|--------| |--------------|
^
|--------| |
| Levels | ---------|
|--------|
|--------| |-------------------|
| Topics | ---> | CategoriesTopics |
|--------| |-------------------|
^
|------------| |
| Categories | ---------|
|------------|
这样,每个主题都有一个行,每个级别有一个行,每个类别有一个行。关系逻辑将包含在一个新的表中,这样一切都会保持干燥。
阅读更多关于许多的信息
https://stackoverflow.com/questions/34277592
复制相似问题