【问题标题】:Build a table which has a Field of User_ids, and then querying for a particular User_Id建立一个具有 User_ids 字段的表,然后查询特定的 User_Id
【发布时间】:2011-02-10 00:14:59
【问题描述】:

我需要一些帮助来构建一个表,然后在 Rails 3 中从该表中获取数据。

下面是分解:

模型 - 这里涉及 3 个模型,它们是:

  • 线程有很多参与者
  • 参与者属于线程
  • 用户

活动表:

id | thread_id | participants

示例记录如下所示:

1 | 300 | 3,1,5,67,13
2 | 333 | 3,12
3 | 433 | 1,12
4 | 553 | 1,12, 67

其中的参与者,是一个 user_ids 列表,如果有更好的方法来存储 user_ids,请告诉我。我还没有建立这个。

在我填充活动表之后。然后我希望能够按照以下方式进行查询: 选择参与者字段中包含 67 的参与者 ID 的所有 Activity 记录。

我希望以上内容很清楚,如果没有,请告诉我。想法?想法?建议。

谢谢

【问题讨论】:

  • 有人吗?混淆一个问题或?

标签: ruby-on-rails ruby-on-rails-3 activerecord activemodel


【解决方案1】:

虽然在列中存储多个值很诱人,但最终总会有人受伤。你最好建立一个连接表来关联模型。

例如你可以这样做:

class DiscussionThread < ActiveRecord::Base
  has_many :participations
  has_many :participants, :through => :participations
end

class Participation < ActiveRecord::Base
  belongs_to :discussion_thread
  belongs_to :participant, :class_name => "User", :foreign_key => :user_id
end

class User < ActiveRecord::Base
  has_many :participations
  has_many :dicussion_threads, :through => :participations
end

这给了你三个表:

table: discussion_threads
columns: id

table: participations
columns: id | discussion_thread_id | user_id

table: users
columns: id

要查找用户参与的线程,只需执行以下操作:

@user.discussion_threads

并找到参与线程的用户:

@discussion_thread.participants

注意:Thread 是 Ruby 中的保留字,所以我将其重命名为 DiscussionThread

编辑

介意展示一个如何序列化 id 数组然后查询它们的示例吗?

你在半夜醒来,在一种奇怪的冲动的力量下,你走到你的电脑前并创建了这个迁移:

rails g model Abomination horror_ids:text

和型号:

class Abomination < ActiveRecord::Base
  serialize :horror_ids
end

你测试它以确保它可以存储一个数组:

@abomination = Abomination.create(:horror_ids=>[2,33,42])
@abomination.horror_ids # => [2,33,42]

那又怎样?你知道 Rails 在后台将其转换为 YAML,如下所示:

---\n
- 2\n
- 33\n
- 42\n

再次被那种奇怪的敦促所驱使,您想知道“我如何搜索存储在此列中的特定 id?”。好吧,它只是一个字符串,对吧?您知道如何在文本字段中找到它:

cthulhu = 33
Abomination.where(["horror_ids LIKE '%- ?\n%'",cthulhu]).first

随着恐惧的增加,您意识到有人可能会偶然发现这一点,并认为这实际上是一个好主意。它必须被摧毁!但你无法输入rm -rf *,而是奇怪的力量让你想到未来克苏鲁的盲目追随者开发者可能需要知道的怪癖,例如

@abomination = Abomination.create
@abomination.horror_ids # => nil

@abomination = Abomination.create(:horror_ids=>[])
@abomination.horror_ids # => []

@abomination = Abomination.create(:horror_ids=>"any string value can go here")
@abomination.horror_ids # => "any string value can go here"

当列大小太小而无法容纳所有数据时,序列化数据可能会损坏。

你拼尽全力想要拔掉电源线,但为时已晚,控制你的胡言乱语、疯狂的意识将代码发布到 StackOverflow 上,让全世界都看到。最后,你陷入了混乱的睡眠。第二天,意识到自己的所作所为,你永远放弃编码,成为一名会计师。

道德

不要这样做

【讨论】:

  • 谢谢 我在大多数情况下同意连接表。但就我而言,我正在使用临时数据构建新闻提要,并且需要避免数据库命中。这就是为什么我要走这条路。与 FB 或 Twitter 等大型网络非常相似。关于如何使上述成为可能的任何想法?
  • 我想说你需要的是良好的性能。你怎么知道避免数据库命中是获得它的方法?您可以在字符串列中序列化一个 ID 号数组。如果你想。呃。
  • 很抱歉让你 ugh :) 介意展示一个如何序列化 id 数组然后查询它们的示例?
  • 你去。使用风险自负。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
相关资源
最近更新 更多