简体   繁体   English

从Rails中的模型实例获取数据

[英]Getting data from an instance of a model in Rails

I am pretty new to Rails and I am lost right now. 我对Rails很陌生,现在迷路了。 I was following the blog tutorial , utilizing the information to create a different application which is kinda similar. 我一直在关注博客教程 ,利用这些信息来创建另一个类似的应用程序。

Well, what I want (in SQL language) is: 好吧,我想要的(SQL语言)是:
select planned from hourplans where area_id = 2 and time = '9:00:00'

There are 4 Models in my Application (it is basically the application from the blog tutorial put twice on the same website): 我的应用程序中有4个模型(基本上是博客教程中的应用程序在同一网站上放置了两次):

Productions (which can have multiple downtimes) [Production] --1-------N-- [Downtime] 生产(可以有多个停机时间) [生产] --1 ------- N-- [停机时间]
class Production < ActiveRecord::Base has_many :downtimes, dependent: :destroy validates :items, presence: true end
Production has the Attributes: Date(date), Time(time), Area(text) and Items(integer) 生产具有以下属性:日期(日期),时间(时间),区域(文本)和项目(整数)

class Downtime < ActiveRecord::Base belongs_to :production end
Downtime has the Attributes Username(text), Category(text), Machine(text), Station(text), Minutes(integer) and Details(text) 停机时间具有用户名(文本),类别(文本),机器(文本),站(文本),分钟(整数)和详细信息(文本)属性

Areas (which can have multiple hourplans) [Area] --1-------N-- [Hourplan] 区域(可以有多个小时计划) [区域] --1 ------- N-- [小时计划]
class Area < ActiveRecord::Base has_many :hourplans, dependent: :destroy validates :title, presence: true end
Area has the Attributes Title(text), Text(text) 区域具有标题(文本),文本(文本)属性

class Hourplan < ActiveRecord::Base belongs_to :area end
Hourplan has the Attributes Time(time), Cycle(decimal), Minutes(integer) and Planned(integer) Hourplan具有时间(时间),周期(十进制),分钟(整数)和计划的(整数)属性

So for example there is an object for the production from 9:00 to 10:00, production has a field called "area" which has the value 'area2'. 因此,例如存在一个从9:00到10:00的生产对象,生产具有一个名为“ area”的字段,其值是“ area2”。
And there is also a object of Area which has the title 'Area2' which is related to an object Hourplan that has a field called 'planned' with the value of items that are planned(like 600) from 9:00 to 10:00. 并且还有一个名为“ Area2”的Area对象,该对象与对象Hourplan有关,该对象的Hourplan具有一个称为“ planned”的字段,该字段的值是从9:00到10:00的已计划项(例如600)。 。
How can I fetch that data into the show-page of the production from 9:00 to 10:00? 我如何从9:00到10:00将这些数据提取到作品的展示页面中?

I can't get it to transfer. 我无法转移。

I tried something like: 我尝试了类似的东西:
<% area2.hourplans.where("time = 09:00").select(:planned) %>

but that didnt work. 但这没用。

Then I thought I had to set the object of the model to a variable first, so I tried: 然后我以为我必须先将模型的对象设置为变量,所以我尝试了:
<% area = Area.find_by title: @production.area %>

But I just dont get it. 但我就是不明白。 I dont know whether this worked or not. 我不知道这是否有效。 But how can I get to the hourplans now? 但是,现在我该如何制定小时计划? Those are related to the Areas 这些与地区有关

I also tried: 我也尝试过:
<%= Hourplan.select("planned").where("time = 9:00 AND area_id = 2") %>
and that just gave me: 那给了我:
#<Hourplan::ActiveRecord_Relation:0x50c23e0> and I dont know what that means, I thought I would have gotten a value out of the SQL Table, like '600' #<Hourplan::ActiveRecord_Relation:0x50c23e0> ,我不知道这意味着什么,我想我应该从SQL表中得到一个值,例如'600'

I hope I made myself clear and understandable. 我希望我能使自己清晰易懂。 If you have any questions please let me know. 如果您有任何疑问,请告诉我。 I would really appreciate your help, I was looking through the official guides but I can't really find what I am looking for. 我很感谢您的帮助,我正在浏览官方指南,但找不到真正想要的东西。

You may try to replace 您可以尝试更换

<%= Hourplan.select("planned").where("time = 9:00 AND area_id = 2") %>

by 通过

<%= Hourplan.where("time = '9:00' AND area_id = 2").first.planned %>

Indeed, the where method returns a relation, which represents (roughly) the result of a SQL query. 实际上, where方法返回一个关系,该关系(大致)表示SQL查询的结果。 But you know that an SQL query may or may not return a single line. 但是您知道SQL查询可能返回也可能不返回一行。 Generally, there are several rows returned. 通常,返回几行。 That's why you'll want to add first , to get only the first Hourplan object. 这就是为什么要添加first ,以仅获取第一个Hourplan对象的原因。

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

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