将预先完成的 SQL 转换为 Rails'风格模块

发布于 2024-09-02 09:32:35 字数 1680 浏览 8 评论 0原文

我是 Rails 新手,如果有人将这些 SQL 转换为 Rails 的完整模块,我将非常感激。我知道有很多问题要问,但我不能只使用 find_by_sql 来解决所有问题。或者我可以吗?

这些是 SQL(它们在 MS-SQL 上运行):

SELECT STANJA_NA_DAN_POSTAVKA.STA_ID, STP_日期, STP_TIME, STA_OPIS, STA_SIFRA, 来自 STANJA_NA_DAN_POSTAVKA 的 STA_POND 内连接 STANJA_NA_DAN ON(STANJA_NA_DAN.STA_ID=STANJA_NA_DAN_POSTAVKA.STA_ID) WHERE ((OSE_ID=10)AND (STANJA_NA_DAN_POSTAVKA.STP_DATE>={d '2010-03-30'}) AND (STANJA_NA_DAN_POSTAVKA.STP_DATE<={d '2010-03-30'}))

选择 ZIGI_OBDELANI.OSE_ID,
ZIGI_OBDELANI.DOG_ID AS DOG_ID,
ZIGI_OBDELANI.ZIO_DATUM 作为基准,
ZIGI_PRICETEK.ZIG_TIME_D AS ZIG_PRICETEK,
ZIGI_KONEC.ZIG_TIME_D AS ZIG_KONEC
来自(ZIGI_OBDELANI 内连接 ZIGI ZIGI_PRICETEK ON ZIGI_OBDELANI.ZIG_ID_PRICETEK = ZIGI_PRICETEK.ZIG_ID )
ZIGI_OBDELANI.ZIG_ID_KONEC 上的内连接 ZIGI ZIGI_KONEC = ZIGI_KONEC.ZIG_ID
哪里(ZIGI_OBDELANI.OSE_ID = 10) AND (ZIGI_OBDELANI.ZIO_DATUM >= {d '2010-03-30'}) AND (ZIGI_OBDELANI.ZIO_DATUM <= {d '2010-03-30'}) AND (ZIGI_PRICETEK.ZIG_VELJAVEN >> 0) 和 (ZIGI_KONEC.ZIG_VELJAVEN <> 0)
ORDER BY ZIGI_OBDELANI.OSE_ID, ZIGI_PRICETEK.ZIG_TIME ASC

这些 SQL 是日常工作时间,我按原样获取它们。我还得到了数据库,它(正如你从 SQL-s 中看到的)不符合 Rails 约定。 作为 PS:

  1. 诸如此类的事情 STP_DATE>={d'2010-03-30'}) 当然是日期 (以斯洛文尼亚语日期表示法)和 将被替换为变量 (日期),以便用户可以 选择日期从和日期到。

  2. 所有这些数据都将显示在 表中的同一页,所以也许 全部在一个模块中?或者很多?如果这个 也许有帮助。

那么有人可以帮助我吗?这是为了我的工作,也是我的第一个项目,我是 Rails 新手,老板们越来越不耐烦了(他们实际上变得相当大声)

非常非常感谢!

因此,如果有人能够为至少一个复杂的 SQL 做一个相关的模型部分,我将非常感激。我将在一个控制器中通过一个操作读取所有数据。一切都将在一次运行中完成。

I am a Rails newbie and would really appreciate if someone converted these SQLs to complete modules for rails. I know its a lot to ask but I can't just use find_by_sql for all of them. Or can I?

These are the SQLs (they run on MS-SQL):

SELECT STANJA_NA_DAN_POSTAVKA.STA_ID,
STP_DATE,
STP_TIME,
STA_OPIS,
STA_SIFRA,
STA_POND FROM STANJA_NA_DAN_POSTAVKA
INNER JOIN STANJA_NA_DAN ON(STANJA_NA_DAN.STA_ID=STANJA_NA_DAN_POSTAVKA.STA_ID)
WHERE ((OSE_ID=10)AND (STANJA_NA_DAN_POSTAVKA.STP_DATE>={d '2010-03-30'})
AND (STANJA_NA_DAN_POSTAVKA.STP_DATE<={d '2010-03-30'}))

SELECT ZIGI_OBDELANI.OSE_ID,
ZIGI_OBDELANI.DOG_ID AS DOG_ID,
ZIGI_OBDELANI.ZIO_DATUM AS DATUM,
ZIGI_PRICETEK.ZIG_TIME_D AS ZIG_PRICETEK,
ZIGI_KONEC.ZIG_TIME_D AS ZIG_KONEC
FROM (ZIGI_OBDELANI
INNER JOIN ZIGI ZIGI_PRICETEK
ON ZIGI_OBDELANI.ZIG_ID_PRICETEK = ZIGI_PRICETEK.ZIG_ID )
INNER JOIN ZIGI ZIGI_KONEC ON ZIGI_OBDELANI.ZIG_ID_KONEC = ZIGI_KONEC.ZIG_ID
WHERE (ZIGI_OBDELANI.OSE_ID = 10)
AND (ZIGI_OBDELANI.ZIO_DATUM >= {d '2010-03-30'})
AND (ZIGI_OBDELANI.ZIO_DATUM <= {d '2010-03-30'})
AND (ZIGI_PRICETEK.ZIG_VELJAVEN <> 0)
AND (ZIGI_KONEC.ZIG_VELJAVEN <> 0)
ORDER BY ZIGI_OBDELANI.OSE_ID, ZIGI_PRICETEK.ZIG_TIME ASC

These SQLs are daily working hours and I got them as is. Also I got Database with it which (as you can see from the SQL-s) is not in Rails conventions.
As a P.S.:

  1. Things like
    STP_DATE>={d'2010-03-30'})
    are of course dates
    (in Slovenian date notation) and
    will be replaced with a variable
    (date), so that the user could
    choose date from and date to.

  2. All of this data will be shown in
    the same page in the table,so maybe
    all in one module? Or many?; if this
    helps, maybe.

So can someone help me? Its for my work and its my 1st project and I am a Rails newbie and the bosses are getting inpatient(they are getting quite loud actually)

Thank you very very much!

So again I would really apreciate if someone could do a relevant model part for at least one of the complicated SQLs. I will read all the data in one controller and in one action. All will be done in one run.

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

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

发布评论

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

评论(3

£烟消云散 2024-09-09 09:32:35

实际上,Rails 处理此问题的方法是为每个表创建模型,并在模型内创建关系。

由于它是遗留模型,您将必须执行以下

class Stanja < ActiveRecord::Base
  set_table_name 'STANJA_NA_DAN'
  set_primary_key 'your-primary-key'
end

class Postavka < ActiveRecord::Base
  set_table_name 'STANJA_NA_DAN_POSTAVKA'
  set_primary_key 'STA_ID'
  has_many :stanjas, :foreign_key => 'sta_id', :primary_key => 'sta_id'
end  

类似操作。不知道你的模型和你的表名到底意味着什么:)
但我希望它能让你开始。

成功!!

Actually the Rails way to handle this, is to create models for each table, and create the relations inside the model.

Since it is a legacy-model you will have to do the following

class Stanja < ActiveRecord::Base
  set_table_name 'STANJA_NA_DAN'
  set_primary_key 'your-primary-key'
end

class Postavka < ActiveRecord::Base
  set_table_name 'STANJA_NA_DAN_POSTAVKA'
  set_primary_key 'STA_ID'
  has_many :stanjas, :foreign_key => 'sta_id', :primary_key => 'sta_id'
end  

something like that. Without knowing your model and what the hell your table-names mean :)
But i hope it gets you started.

Success!!

紫竹語嫣☆ 2024-09-09 09:32:35

在数据库中为这些 sql 创建视图,并为每个视图创建模型。

create views for these sqls in the database and a model for each view.

卷耳 2024-09-09 09:32:35

这并不是真正要求人们“为我做我的工作”的地方,还有其他网站,例如rent-a-coder,您可以在那里做到这一点。

不过,这是一个寻求帮助的地方,我可以给你一些指导。

您应该做的第一件事就是开始弄清楚您的模型是什么。您可以尝试使用 Dr Nics 魔法模型 自动生成模型,但我希望数据库不遵循 ActiveRecord 约定这一事实可能对您来说是个问题。

过去,我通过将表和列重命名为 Rails 约定并提供反映现有遗留应用程序使用的当前命名约定的视图,在遗留数据库方面取得了一些成功。如果您能够做到这一点,那么您的生活会更轻松,因为您可以使用 Magic Models 或 Ryan 等生成器Bates 漂亮的脚手架为您提供一个基本结构,然后开始工作。

希望这对您有帮助,并祝您的项目顺利。

This is not really the place to ask people to "Do my work for me" there are other sites like rent-a-coder where you can do that.

This is a place to ask for help however and I can give you some pointers.

The first thing you should do is start to work out what your models are. You could try auto-generating your models using Dr Nics magic models though I expect that the fact that the database does not follow ActiveRecord conventions may be a problem for you.

I have had some success in the past with legacy databases by renaming the tables and columns to Rails conventions and providing a view that reflects the current naming convention for the existing legacy applications to use. If you were able to do that then your life would be easier as you could then use generators like Magic Models or Ryan Bates nifty scaffold to provide you a basic structure to then start working on.

Hope this helps and good luck with your project.

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