简体   繁体   English

如何在Rails网页上的jruby中间查询数据库?

[英]How do you query a database in the middle of a jruby on rails webpage?

Not sure how to ask it so sorry if I mess up my terminology. 不知道该怎么问,对不起,如果我弄乱了我的术语。 In using jruby on rails, how would I query two (or more database) to serve back to the view page? 在Rails上使用jruby时,我将如何查询两个(或多个数据库)并返回视图页面?

I have seen where I set up my database connection in database.yml and it works fine but I am now wondering how I move beyond this to hitting many databases with jdbc and puttting them in one webpage view. 我已经看到了在database.yml中建立数据库连接的位置,它可以正常工作,但是我现在想知道如何超越此范围,使用jdbc打入许多数据库并将其放入一个网页视图中。

My title is worded the way it is because I am used to a non-orm non-mvc way of doing things. 之所以这样称呼我的标题,是因为我习惯了一种非orm非mvc的处理方式。 For example, need to query somethingn in classic asp? 例如,需要查询经典ASP中的somethingn吗? Just create the object and emit html and your dataset for each database. 只需创建对象并为每个数据库发出html和数据集即可。 Cumbersome but at least I know how to do it. 繁琐,但至少我知道该怎么做。 Need to hit five databases in asp.net? 需要在asp.net中访问五个数据库吗? Just have a control and in the code-behind bind it to the table adapter or do a sql query. 只需拥有一个控件,然后在代码背后,将其绑定到表适配器或执行sql查询即可。 Here, I'm just lost on how to do this. 在这里,我只是迷失了如何执行此操作。

How do I do query several databases, different ones (oracle, sql server, etc.) in Jruby on Rails with jdbc and put them all in one page? 如何使用jdbc在Rails的Jruby中查询几个数据库,不同的数据库(Oracle,SQL Server等),并将它们全部放在一个页面中? And do this the "right" way so that I don't end up with my jror application looking like classic asp. 并以“正确”的方式执行此操作,以免最终我的jror应用程序看起来像经典的asp。

EDIT: I think something like this might be what I am looking for, but I am not sure how to put both sets of data on the same page. 编辑:我认为像这样可能是什么我期待的,但我不知道如何把这两组数据在同一页上。

Put the query logic in the appropriate models (activerecord models for your main db tables, non-activerecord models for other sources of data). 将查询逻辑放入适当的模型中(主db表的activerecord模型,其他数据源的非activerecord模型)。 These should have intention revealing method names. 这些应该有意图揭示方法名称。

In the controller action query for the information your web page needs. 在控制器操作中查询您的网页所需的信息。 Load these data into instance variables. 将这些数据加载到实例变量中。

In the view, interact simply with the instance variables to display them using ERB. 在视图中,只需与实例变量进行交互即可使用ERB显示它们。 If you find logic in your views, you're doing it wrong and should refactor. 如果您在视图中发现逻辑,则说明您做错了,应该重构。

For multiple DBs, this may be of help http://tomayko.com/writings/rails-multiple-connections 对于多个DB,这可能会有所帮助http://tomayko.com/writings/rails-multiple-connections

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM