首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 4 belongs_to将in存储在数组列中

Rails 4 belongs_to将in存储在数组列中
EN

Stack Overflow用户
提问于 2015-12-14 22:13:14
回答 1查看 624关注 0票数 1

我有一个主题模型,它属于分类

知道每个主题属于最大的5-6级和2-3个类别,是否可以使用数组将每个主题的类别it和级别it存储在2列中:true of Postgresql,或者这样做是不好的做法?

EN

回答 1

Stack Overflow用户

发布于 2015-12-14 23:00:34

has_and_belongs_to_many的关系对你有用吗?

代码语言:javascript
运行
复制
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

这将使结构看起来像:

代码语言:javascript
运行
复制
|--------|      |--------------|
| Topics | ---> | LevelsTopics |
|--------|      |--------------|
                    ^
|--------|          |
| Levels | ---------|
|--------|


|--------|      |-------------------|
| Topics | ---> | CategoriesTopics  |
|--------|      |-------------------|
                        ^
|------------|          |
| Categories | ---------|
|------------|

这样,每个主题都有一个行,每个级别有一个行,每个类别有一个行。关系逻辑将包含在一个新的表中,这样一切都会保持干燥。

阅读更多关于许多的信息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34277592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档