Rails3 以及使用关联的正确方法

发布于 2024-11-15 23:50:46 字数 2121 浏览 5 评论 0原文

我正在做我的第一个rails(3) 应用程序。

协会没有意义。首先,即使是导轨也不 真正解释他们的作用,他们只是解释如何使用它们。 据我所知,关联有两件事:

    a) Allow ActiveRecord to optimize the structure of the database.
    b) Allow ActiveRecord to offer an alternate ruby syntax for
       joins and the like (SQL queries). I want this.

我试图理解关联,以及如何正确使用它们。基于 在下面的例子中,关联似乎被“破坏”了,或者至少是 文档是。

考虑我的应用程序的一个简单版本。老师修改单词表 用于学习。

此讨论有 3 个相关表格。为了清楚起见,我简单地 包括 annotate(1) 工具对表的定义,并删除 不必要的字段/列。

单词列表管理表:

    Table name: wordlist_mgmnt_records
    id         :integer         not null, primary key
    byline_id  :integer(8)      not null

将单词映射到单词列表的表:

    Table name: wordlists
    wordlist_mgmnt_id :integer         not null
    word_id           :integer         not null

我们实际上并不关心单词本身。但我们确实关心 最后一个表,署名:

    Table name: bylines
    id           :integer(8)      not null, primary key
    teacher_id   :integer         not null
    comment      :text            not null

署名记录了谁、使用了什么工具、在哪里、何时等。署名是 主要用于解决发生的问题,以便我可以向用户解释什么 他们应该这样做(和/或修复他们的错误)。

教师可以一次修改一个或多个单词表管理记录 (又名单署名)。换句话说,单个更改可能会更新多个 单词列表。

对于 wordlist_mgmnt_records ,关联将是:

    has_many :bylines       # the same byline id can exist
                            # in many wordlist_mgmnt_records

但是署名的相应条目是什么?

Beginning Rails 3(Carneiro 等人)一书中说:

    "Note: For has_one and has_many associations, adding a belongs_to
    on the other side of the association is always recommended. The
    rule of thumb is that the belongs_to declaration always goes in
    the class with the foreign key."

[是的,我还为此查看了在线 Rails 指南。没有 帮助。 ]

对于署名表/类我真的想说吗?

    belongs_to :wordlist_mgmnt_records

这确实没有道理。署名表基本上属于_to 数据库中的每个表都有一个 bylines_id。那么我真的会说 属于他们所有人?那不是在所有的目录中都设置了外键吗? 其他表?这反过来又会使改变变得更加昂贵(太多 CPU 周期)比我真正想要的。有些更改涉及很多表,有些 它们非常大。我看重正常使用时的速度,并愿意等待 使用署名进行清理/修复时查找没有外键的署名。

这让我们回到了原点。协会在铁路领域真正做什么? 以及如何明智地使用它们?

仅仅因为可以而使用关联似乎并不正确 答案,但是如何获得添加的连接语法呢?

I'm doing my first rails(3) application.

Associations don't make sense. First, even the rails guides don't
really explain what they do, they just explain how to use them.
From what I gather, associations do two things:

    a) Allow ActiveRecord to optimize the structure of the database.
    b) Allow ActiveRecord to offer an alternate ruby syntax for
       joins and the like (SQL queries). I want this.

I'm trying to understand associations, and how to properly use them. Based
on the example below, it seems like associations are 'broken' or at least
the documentation is.

Consider a trivial version of my application. A teacher modifying wordlists
for study.

There are 3 relevant tables for this discussion. For clarity, I've simply
included the annotate(1) tool's definition of the table, and removed
unnecessary fields/columns.

A wordlist management table:

    Table name: wordlist_mgmnt_records
    id         :integer         not null, primary key
    byline_id  :integer(8)      not null

A table that maps words to a word list:

    Table name: wordlists
    wordlist_mgmnt_id :integer         not null
    word_id           :integer         not null

We don't actually care about the words themselves. But we do care about
the last table, the bylines:

    Table name: bylines
    id           :integer(8)      not null, primary key
    teacher_id   :integer         not null
    comment      :text            not null

Bylines record who, what tool was used, where, when, etc. Bylines are
mainly used to trouble shoot what happened so I can explain to users what
they should have done (and/or repair their mistakes).

A teacher may modify one or more word list management records at a time
(aka single byline). Said another way, a single change may update multiple
word lists.

For wordlist_mgmnt_records the associations would be:

    has_many :bylines       # the same byline id can exist
                            # in many wordlist_mgmnt_records

But what's the corresponding entry for bylines?

The Beginning Rails 3 (Carneiro, et al) book says:

    "Note: For has_one and has_many associations, adding a belongs_to
    on the other side of the association is always recommended. The
    rule of thumb is that the belongs_to declaration always goes in
    the class with the foreign key."

[ Yes, I've also looked at the online rails guide(s) for this. Didn't
help. ]

For the bylines table/class do I really want to say?

    belongs_to :wordlist_mgmnt_records

That really doesn't make sense. the bylines table basically belongs_to
every table in the data base with a bylines_id. So would I really say
belongs_to all of them? Wouldn't that set up foreign keys in all of the
other tables? That in turn would make changes more expensive (too many
CPU cycles) than I really want. Some changes hit lots of tables, some of
them very large. I prize speed in normal use, and am willing to wait to
find bylines without foreign keys when using bylines for cleanup/repair.

Which brings us full circle. What are associations really doing in rails,
and how does one use them intelligently?

Just using associations because you can doesn't seem to be the right
answer, but how do you get the added join syntax otherwise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2024-11-22 23:50:46

我会尽力帮助您解决困惑......

一个署名可以有多个wordlist_mgmnt_records,因此在那里定义has_many似乎是有意义的。

我不确定我是否理解您在另一个方向上的困惑。由于您已经定义了属性 wordlist_mgmnt_records.byline_id,因此任何给定的 wordlist_mgmnt_record 只能“具有”(属于)单个署名线。您只需通过 ruby​​ 定义鱼尾纹(如果您喜欢数据库图表):

wordlist_msgmnt_records (many)>>----------(one) byline

或者用英语阅读:“一个 byline 可以有许多 wordlist_mgmnt,并且许多单独的 wordlist_mgmnt 可以属于单个 byline”

将 Belongs_to 定义添加到 wordlist_mgmnt 模型不会影响查询的性能,它只是让您执行以下操作:

@record = WordlistMgmntRecord.find(8)
@record_byline = @record.byline

此外,您还可以对表进行联接,例如:

@records = WordlistMgmntRecord.joins(:byline).where({:byline => {:teacher_id => current_user.id}})

这将执行以下 SQL:

SELECT wordlist_mgmnt_records.*
FROM wordlist_mgmnt_records
INNER JOIN bylines
  ON wordlist_mgmnt_records.byline_id = bylines.id
WHERE bylines.teacher_id = 25

(假设 current_user.id 返回 25)

这基于您当前的数据库设计。如果您发现有一种方法可以实现您想要的功能,而无需将 byline_id 作为 wordlist_mgmnt_records 表中的外键,那么您可以修改您的模型可以容纳它。然而,这似乎是规范化数据库应有的样子,我不太确定您还会采取什么其他方式来做到这一点。

I'll try to help your confusion....

A byline can have multiple wordlist_mgmnt_records, so defining the has_many there seems to make sense.

I'm not sure I understand your confusion in the other direction. Since you have defined the attribute wordlist_mgmnt_records.byline_id, any given wordlist_mgmnt_record can only 'have' (belong_to) a single byline. You're simply defining the crows foot via ruby (if you like database diagrams):

wordlist_msgmnt_records (many)>>----------(one) byline

Or read in english: "One byline can have many wordlist_mgmnts, and many individual wordlist_mgmnt's can belong to a single byline"

Adding the belongs_to definition to the wordlist_mgmnt model doesn't affect the performance of the queries, it just let's you do things like:

@record = WordlistMgmntRecord.find(8)
@record_byline = @record.byline

Additionally you're able to do joins on tables like:

@records = WordlistMgmntRecord.joins(:byline).where({:byline => {:teacher_id => current_user.id}})

Which will execute this SQL:

SELECT wordlist_mgmnt_records.*
FROM wordlist_mgmnt_records
INNER JOIN bylines
  ON wordlist_mgmnt_records.byline_id = bylines.id
WHERE bylines.teacher_id = 25

(Assuming current_user.id returned 25)

This is based off of your current DB design. If you find that there's a way you can implement the functionality you want without having byline_id as a foreign key in the wordlist_mgmnt_records table then you would modify your models to accomodate it. However this seems to be how a normalized database should look, and I'm not really sure what other way you would do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文