Ruby/Rails - 访问“查找"表不用建模吗? [英] Ruby/Rails - Access a "lookup" table without modeling it?

查看:49
本文介绍了Ruby/Rails - 访问“查找"表不用建模吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 Ruby on Rails 3.0.x 项目.

This is for a Ruby on Rails 3.0.x project.

我有一个查找"表,其中包含来自供应商的数据.

I have a "lookup" table with data from a vendor.

当我从其他来源导入数据时,我想检查此表(加入 SKU)以获取额外数据.

When I import data from another source, I want to check this table (join on a SKU) for extra data.

在我的应用程序中为这张表创建模型对我来说似乎不合适.我的应用程序永远不会更改数据,除了我刚刚提到的数据查找之外,它不需要任何模型关联.它只是偶尔访问以检查一些信息.

It doesn't seem right to me to create a model for this table in my app. The data will never be changed by my app, and it doesn't need to have any model associations beyond the data lookup I just mentioned. It's just occasionally accessed to check for some info.

访问此表的最佳做法是什么?

What is the best practice to access this table?

谢谢.

推荐答案

围绕它创建模型没有坏处,但如果你想避免它,你将不得不向数据库发送原始 SQL 查询以获得数据返回作为替代.

There's no harm in creating a model around it, but if you'd like to avoid it, you will have to send raw SQL queries to the DB to get data back as an alternative.

原始查询:Rails 原始 SQL 示例

另一方面,我认为围绕模型包装模型的动机不仅仅是您是否要修改它.我肯定有一些应用程序的模型围绕着永远不会改变的表格(即应用程序特定术语列表、城市/州列表以及其他一次导入且永不更改的静态数据).ActiveRecord 还将列包装成易于访问的方法,因此您可以轻松读取该数据,更不用说排序、查找等所有方便的方法了.

On the other hand, I think the motivation for wrapping a model around it goes beyond whether or not you want to modify it. I definitely have apps that have models around tables that never change (i.e. a list of app-specific terminology, a list of cities/states, and other static data that is imported once and never changed). ActiveRecord will also wrap the columns into easily accessible methods, so you can read that data easily, not to mention all the convenience methods around sorting, finding, etc.

这篇关于Ruby/Rails - 访问“查找"表不用建模吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆