Rails has_and_belongs_to_many 或手动实现

发布于 2024-09-12 08:00:41 字数 466 浏览 2 评论 0原文

我正在为一家药房设计一个 Ruby on Rails 应用程序,其中一个功能是有些商店有药剂师在那里工作。此外,还有药剂师,可以在很多商店工作。这听起来像是 HABTM 的工作,对吧?好吧,作为新手,我手动设计了一个解决方法(因为我从未听说过 HABTM - 我基本上自学了 Rails,但从未接触过一些更高级的关系)。现在,当保存药剂师时,药剂师控制器的创建和更新操作中有几行将他们工作的商店转换为字符串,每个 store_id 用逗号分隔。然后,当显示商店时,它会通过以下方式发出 MYSQL 请求: @pharmacists = Pharmacist.find :all, :conditions => "stores REGEXP '#{@store.id}'"

将此系统转移到基于 Rails 的 HABTM 系统会更高效吗?当然,最终需要的代码会更少,但是值得吗?换句话说,除了更少的代码之外,通过将此关联移至 Rails 管理,我还能获得什么好处?

I'm designing a ruby on rails app for a pharmacy, and one of the features is that there are stores who have pharmacists who work there. In addition, there are pharmacists, who can work at many stores. This sounds like a job for HABTM, right? Well, being the novice I am, I manually designed a workaround (because I never heard of HABTM - I basically taught myself rails and never got to some of the more advanced relationships). Right now, when a pharmacist is saved, there's a couple of lines in the create and update action of the pharmacists controller that turns the stores that they work at into a string, with each store_id separated by a comma. Then, when a store is displayed, it does a MYSQL request by
@pharmacists = Pharmacist.find :all, :conditions => "stores REGEXP '#{@store.id}'"

Would moving this system over to a rails based HABTM system be more efficient? Of course it would require less code in the end, but would it be worth it? In other words, what benefits, other than less code, would I get from moving this association to be managed by rails?

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

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

发布评论

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

评论(1

情绪 2024-09-19 08:00:41

好处是您将使用正确的工具来完成工作!使用 Rails 等框架的全部意义在于,它可以帮助您解决常见问题,而无需重新发明轮子,而这正是您在这里所做的。通过使用关联,您还可以正确使用关系数据库,并可以利用外键索引等优势,这比字符串操作更快。

您应该使用 has_and_belongs_to_many 关系,除非您需要在连接模型上存储额外的属性(例如药剂师开始在商店工作的日期),在这种情况下使用 has_many :through

使用 Rails 关联将为您提供 Rails 提供的所有便捷方法,例如:

# Find the stores the first pharmacist works at
@stores = Pharmacist.first.stores

# Find the pharmacists who work at a store
@pharmacists = Store.find_by_name('A Store').pharmacists

The benefit is that you will be using the right tool for the job! The whole point of using a framework such as Rails is that it helps you solve common problems without having to re-invent the wheel, which is what you've done here. By using associations you'll also be using a relational database properly and can take advantage of benefits like foreign key indexing, which will be faster than string manipulation.

You should use a has_and_belongs_to_many relationship unless you need to store extra attributes on the join model (for example the date a pharmacist started working at a store) in which case use has_many :through.

Using Rails associations will give you all the convenient methods that Rails provides, such as these:

# Find the stores the first pharmacist works at
@stores = Pharmacist.first.stores

# Find the pharmacists who work at a store
@pharmacists = Store.find_by_name('A Store').pharmacists
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文