简体   繁体   中英

Getting data from an instance of a model in Rails

I am pretty new to Rails and I am lost right now. 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:
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):

[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)

[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)

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'.
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.
How can I fetch that data into the show-page of the production from 9:00 to 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'

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. But you know that an SQL query may or may not return a single line. Generally, there are several rows returned. That's why you'll want to add first , to get only the first Hourplan object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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