在rails应用程序中通过has_many在哪里设置外键关系

发布于 2024-10-11 16:24:05 字数 482 浏览 7 评论 0原文

我正在通过关系设置一个简单的 has_many 。我想知道在设置外键关系时是否应该考虑任何最佳实践。

该应用程序旨在允许用户创建商品和广告,其中列表模型用于将商品与广告连接起来(列表模型还具有时间戳和订单字段)。

我的主要问题是:哪些模型应该属于用户模型?我认为最简单的解决方案是列出“属于用户”。这样我就可以使用 has_many through 关系来找出哪些项目和哪些广告属于每个用户。

然而,我突然想到,这可能会留下一些漏洞,具体取决于我想要实现的工作流程。例如,如果用户想要在创建包含这些项目的广告之前创建一堆项目,该怎么办?如果用户在创建任何项目之前创建广告怎么办?

基于上述,我想也许我应该让广告和项目属于用户。如果我这样做,是否有任何明显的理由也使列表属于用户?我本来打算这样做,但不知怎的,这对我来说似乎是多余的。

这似乎是在 Rails 应用程序中遇到的非常典型的情况,所以我希望有经验的人曾经在这里并且可能能够分享一些见解。

谢谢!

I am setting up a simple has_many through relationship. I was wondering if there are any best practices I should consider when setting up the foreign key relationships.

The application is designed to allow users to create items and ads, where a listing model is used to connect items with ads (the listing model also has timestamps and an order field).

The main question I have is: which models should belong_to the user model? I was thinking that the simplest solution is to have listing belong_to user. That way I can use the has_many through relationship to figure out which items and which ads belong_to each user.

However, it occurred to me that this could leave some holes depending on what work-flows I want to make possible. For example, what if a user wants to create a bunch of items before creating an ad which has those items? What if a user creates an ad before having created any items?

Based on the above, I was thinking that maybe I should just make ads and items belong to user. If I do that, is there any obvious reason to also make listing belong_to user? I was going to do that, but somehow it just looked redundant to me.

This seems like a pretty typical situation to encounter in a rails app, so I was hoping somebody with experience has been here and might be able to share some insight.

Thanks!

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

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

发布评论

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

评论(1

想挽留 2024-10-18 16:24:05

我试图了解你拥有什么:

用户
广告
物品
列表

其中Listing 似乎是某种将ItemAd 中的Item 相关联的连接模型。

我不认为这样做有什么错误的方法。我认为最标准化的方法是让一个项目属于一个用户,并通过它获取列表和广告。但您也会在那里付出代价,既必须编写烦人的代码,又必须执行多个连接才能获取广告。

相反的极端是让一切都属于用户。您的两个模型中有一个额外的列,但您也有更简单的关系。在设计架构时,您也不必担心用户可以使用哪些工作流程。

即使您弄乱并遗漏了模型中的 user_id 字段,稍后使用迁移添加这些字段也不是很难。

I'm trying to understand what you have:

User
Ad
Item
Listing

Where Listing appears to be some kind of join model that relates an Item to being in an Ad.

I don't see much of a wrong way to do this. I suppose the most normalized way of doing this would be to just have an Item belong_to a User, and get the Listings and Ads through that. But you'd be paying a price there too, both in having to write annoying code and having to perform multiple joins to get your Ads.

The opposite extreme is just to have everything belong_to the user. You have an extra column in 2 of your models, but then you also have much simpler relationships. You also wouldn't have to worry about what workflows your users are allowed when designing your schema.

Even if you mess up and leave out user_id fields in your models, it isn't terribly hard to add those fields later using migrations.

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