Rails 4 - 最佳实践 - 如何使用来自不同控制器的数据显示视图?(Rails 4 - Best practices - How to display a view with data from different controllers?)
我正在构建一个Rails 4应用程序。
该应用程序用于为一堆客户端(IT咨询业务将使用的应用程序)记录网络硬件设备。 第一个索引视图是列出所有客户端的视图。 此时,您可以单击链接以查看特定于该客户端的项目。 我已经为我正在记录的不同类型的东西(即服务器控制器,防火墙控制器等)构建了不同的模型和控制器。
我已经到了嵌套的地步,我可以从客户端索引页面获取链接以传递client.id并在该SPECIFIC控制器上查看与该客户端关联的项目(即它将列出该客户端的所有服务器) 。 现在,当你在那里时,我希望它也列出来自不同型号/控制器的所有其他项目。 我想要一个显示来自不同型号/控制器的数据的页面。
我正在尝试确定这样的场景的最佳实践。 我觉得我错过了显而易见的事情,并且Rails以某种特定的方式处理这个问题 - 或者 - 大多数人说Rails糟透了/不这样做,每个人都做X来完成这个任务。
以下是我没有特别顺序的想法:
- 使用partials - 似乎不合适,因为我理解的是为了简化你的视图代码
- 渲染 - 这里关注的是如何传递ID以获取正确的客户端
- 在控制器中操作之前嵌套/使用以设置client.id,然后使用所有其他控制器的实例变量编写索引操作。 已经为嵌套执行此操作并且它可以工作,但仅适用于包含client.id的原始请求链接。 我无法弄清楚如何将client.id传递给其他实例变量,以便显示正确的数据。 我觉得我可以解决这个问题,但似乎我走错了路。
- JavaScrip / AJAX类型的解决方案真的让动态内容发生?
我想确保我在干爽的DRY意义上做到这一点。
I'm building a Rails 4 application.
The application serves to document network hardware devices for a bunch of clients (an app an IT consulting business would use). First index view is one that lists all the clients. At that point you can click on a link to view items specific to that client. I've built out different models and controllers for the different types of things I'm documenting (i.e. servers controller, firewalls controller, etc.).
I've gotten to the point with nesting where I can get the link from the client index page to pass along the client.id and view items associated with that client on that SPECIFIC controller (i.e. it will list all the servers for that client). Now when you are there I want it to also list all the other items that are from different models/controllers as well. I want one page that displays data from the different models/controllers.
I'm trying to determine what best practice is for a scenario like this. I feel like I'm missing the obvious and that Rails deals with this in some specific way - or - that most people say Rails sucks/does not do this and everyone does X to accomplish this task.
Here are the ideas I have in no particular order:
- Use partials - seems out of place as partials as I understand are to simplify your view code
- Render - concern here is how do you pass the ID to get the correct client
- Nesting/Use before action in the controller to set the client.id and then write index actions with instance variables for all the other controllers. Already doing this for the nesting and it works, but just for the original request link that contained the client.id. I can't figure out how to pass the client.id to the other instance variables so the correct data is displayed. I feel like I could fix this bug but it seems like I'm going down the wrong path.
- JavaScrip/AJAX type of solution to really make dynamic content happen??
I want to make sure I'm doing this in the good ole fashioned DRY sense.
原文:https://stackoverflow.com/questions/26941962
最满意答案
您当前的select语句应该引发错误,因为在使用group by时,select子句中只存在group by子句或aggrigation函数中存在的列。
正确的查询非常类似于您发布的查询,只是没有组和正确的显式连接(隐式连接已经过时了20多年),并将这些连接转换为左连接:
SELECT p.pId, p.pName, s.sId, s.sName, g.gId, g.gName FROM table1 as p, LEFT JOIN table2 as s ON p.pId=s.pId LEFT JOIN table3 as g ON s.sId=g.sId
这应该会得到以下输出:
pId pName sId sName gId gName 1 p1 11 s1 111 g1 1 p1 11 s1 112 g2 2 p2 12 s1 null null
Your current select statement should raise an error, since when using group by, only columns that exists in the group by clause or in an aggrigation function can exist in the select clause as well.
The correct query is very much like the one you posted, just without the group by and with proper, explicit joins (implicit joins are outdated for over 20 years now), and turning these joins to left joins:
SELECT p.pId, p.pName, s.sId, s.sName, g.gId, g.gName FROM table1 as p, LEFT JOIN table2 as s ON p.pId=s.pId LEFT JOIN table3 as g ON s.sId=g.sId
This should get you the following output:
pId pName sId sName gId gName 1 p1 11 s1 111 g1 1 p1 11 s1 112 g2 2 p2 12 s1 null null
相关问答
更多-
您当前的select语句应该引发错误,因为在使用group by时,select子句中只存在group by子句或aggrigation函数中存在的列。 正确的查询非常类似于您发布的查询,只是没有组和正确的显式连接(隐式连接已经过时了20多年),并将这些连接转换为左连接: SELECT p.pId, p.pName, s.sId, s.sName, g.gId, g.gName FROM table1 as p, LEFT JOIN table2 as s ON p.pId=s.pId LEFT JOIN ...
-
如果您要查找两个表之间相同的列名,则应该可以: select name from syscolumns sc1 where id = object_id('table1') and exists(select 1 from syscolumns sc2 where sc2.name = sc1.name and sc2.id = object_id('table2')) 你也可以通过在子查询中抛出sc1.xtype = sc2.xtype来确保它们是相同的类型。 If you're looking for ...
-
我使用这个sql来获取所有的死锁。 DECLARE @xml XML SELECT @xml = target_data FROM sys.dm_xe_session_targets JOIN sys.dm_xe_sessions ON event_session_address = address WHERE name = 'system_health' AND target_name = 'ring_buffer' SELECT CAST(XEventData.XEvent.value('(d ...
-
你正在寻找一个join 。 根据您所描述的内容,查询将如下所示: select t1.Model, t2.name, t1.year, t1.code1, t2.code2, t1.year, t2.serial, t1.type from t3 join t1 on t1.Code = t3.Code and t1.Year = t3.Year and t1.Type = t3.Type join t2 on t2.Co ...
-
你的问题对DATEPART作用有明显的误解,以及如何比较日期。 所以让我们分解你的WHERE子句。 where datepart(yy,doe)>= datepart(yy,dateadd(yy,-2,'2012/04/10')) 本条款将列中日期的年份值与固定日期前两年的日期年份(代表用户提交的日期)进行比较。 因此, datepart(yy, dateadd(yy, -2,'2012/04/10'))以int返回2010的值。 这与datepart(yy,doe) ,它返回存储在doe列中的 ...
-
使用LEFT JOIN ,您只需要在至少一个表中验证user_id是否为NOT NULL : SELECT [user].user_id FROM [user] /* LEFT JOIN against each of the 3 tables */ LEFT JOIN full_time_job ft ON [user].user_id = ft.user_id LEFT JOIN part_time_job pt ON [user].user_id = pt.user_id LE ...
-
您正在寻找LEFT JOIN : select t1.*, t2.status from table1 t1 left join table2 t2 on t1.customer = t2.customer; You are looking for LEFT JOIN: select t1.*, t2.status from table1 t1 left join table2 t2 on t1.customer = t2.customer;
-
您应该使用内部联接来获取所需的结果,而不是此处的Distinct关键字。 尝试这个 SELECT b.BRANCHID, b.BRANCHSUBURB, m.MOVIENAME, sc.SCREENCAPACITY, se.SESSIONDATE FROM SESSIONS se INNER JOIN SCREEN sc on se.SCREENID = sc.SCREENID INNER JOIN BRANCH b ON b.BRANCHID = se.BRNACHID INNER JOIN MOVIE ...
-
所以我终于找到了解决方案。 with fks as ( select fk.name AS 'FKName', o_p.name AS 'ParentNameTable', c_p.name AS 'ParentNameColumn', o_r.name AS 'ReferencedNameTable', c_r.name AS 'ReferencedNameColumn', fkc.* from sys.foreign_key_columns fkc ...
-
SQL Server:将所有表的所有列详细信息输出到文件中(SQL Server: output all column details for all tables into a file)[2022-02-12]
尝试使用INFORMATION_SCHEMA.COLUMNS而不是SYS.COLUMNS SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARacter_maximum_length 'Max Length' FROM INFORMATION_SCHEMA.columns GO 还有其他专栏,但这些将帮助您入门。 您还可以尝试使用osql( Link )从 ...