Rails:对于这个问题,单表是一个好的设计选择吗?
我最近刚刚继承了一个 Rails 应用程序,并且正在讨论下一步的架构决策。这是一些背景信息...感谢反馈。
目前有 16 种不同的广告类型,全部具有一组相同的属性,几种具有一个或两个附加属性,还有一些具有三个或四个属性。
目前,每种广告类型都有一个单独的模型和带有相应列的表,以及 CMS 中也用于基本 CRUD 的单独控制器和视图。 CMS 利用继承资源,因此限制了一些重复。
我写出了属性集——大约有 20 个,涵盖了所有广告类型。某些广告类型具有关联 - 一些 has_many 关联(其中外键存储在关联表中)和一些 has_many 关联。
这些广告没有实际行为。特定的广告类型简单地显示在各个页面上,因此只要我能够确定是否存在特定类型的广告,我们就可以了。
我正在讨论转向单个模型、表和控制器,但希望从 stackoverflow 社区获得尽可能多的输入,了解这是否 1) 非常适合问题 2) 任何性能问题 3) 任何潜在的问题我还没有想到的编程瓶颈。到目前为止,这是我的想法...
假设路线 /:location/:ad_type/new (例如 /homepage/ad_type_1/new):
ad_controller 将创建 @ad,并将 ad_type 设置为 params[:location] + params[:ad_type ] 并渲染新视图,其中包含一系列条件,用于显示给定 ad_type 的适当部分。提交将触发创建操作,使用为广告类型定义的预期属性创建广告,在本例中,其中之一为 ad_type = homepage_ad_type_1。
我没有过多考虑检索数据,但假设 ad_type 列设置正确,我应该能够创建一个“of_type”范围,根据 ad_type 列提取记录。不确定我是否遗漏了一些东西。
验证将根据 ad_type 值而有所不同。
我不完全确定在任何给定时间会有多少广告,但我觉得这个问题可以稍后解决。通过将一些过时的行移至 ad_archives 表或类似表中。
感谢您的想法。谢谢。
I just recently inherited a Rails application and am debating an architectural decision moving forward. Here's a bit of background... Feedback appreciated.
There are, currently, 16 different ad types, all with a set of the same attributes, several with one or two additional attributes, and a couple with three or four.
Currently each ad type has a separate model and table with the respective columns, and a separate controller and views in a CMS too used for basic CRUD. The CMS takes advantage of inherited_resources, so that limits some of the duplication.
I wrote out the attribute set -- there are 20 or so that cover all ad types. Certain ad types have associations -- several has_many associations where the foreign key is stored on the associated table and a few belongs_to.
The ads have no real behavior. Specific ad types are simply displayed on various pages, so as long as I can determine if there is an ad of a specific type, we're golden.
I am debating moving to a single model, table, and controller, but want to get as much input as possible from the stackoverflow community on whether or not this is 1) a good fit for the problem 2) any performance concerns 3) any potential programming bottlenecks I haven't though of. Here is my thinking so far...
Assuming a route /:location/:ad_type/new (e.g. /homepage/ad_type_1/new):
The ad_controller would create @ad with ad_type set to params[:location] + params[:ad_type] and render the new view which would contain a series of conditionals for displaying the appropriate partials for the given ad_type. Submit would fire the create action creating the ad with the expected attributes defined for the ad type, one of which in this case would be ad_type = homepage_ad_type_1.
I've not thought as much about retrieving the data, but assuming the ad_type column is set correctly, I should be able to create an "of_type" scope that pulls records based on the ad_type column. Not sure if I'm missing something there.
Validations will vary based on the ad_type value.
I'm not entirely certain how many ads will exist at any given time, but I feel this could be addressed at a later time. Either by moving off some of the stale rows to an ad_archives table or similar.
Your thoughts are appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会为此使用单个表模型,但如果使用多个控制器/视图有意义,您不必将自己限制为单个控制器,并且您仍然可以根据广告的类型查询广告,而无需使用条件。示例:
因此,制作教师控制器和学生控制器以及人员控制器将很容易。
I would probably use a single table model for this, but if it makes sense to use multiple controllers/views, you don't have to restrict yourself to a single controller, and you can still query ads based on their type without using conditionals. Example:
So it would be easy to make a Teachers controller and a Students controller, as well as a People controller.