简体   繁体   English

从控制器循环遍历Rails数据对象

[英]looping through a rails data object from the controller

I have called a function from a class to find all the items related to a particular ID in a many to many HABTM relationship. 我从一个类中调用了一个函数,以查找具有多对多HABTM关系的特定ID的所有项。

Procedures -> Tasks with a join table: procedures_tasks 过程->具有连接表的任务:procedures_tasks

I call the information like @example = Procedure.get_tasks(1,1) 我称信息为@example = Procedure.get_tasks(1,1)

I would like to be able to iterate through the data returned so that I can create an instance of each task_id related to the procedure in question 我希望能够遍历返回的数据,以便可以创建与所讨论过程相关的每个task_id的实例

  def self.get_tasks(model_id, operating_system_id)
    find(:first,  :select => 'tasks.id, procedures.id', :conditions => ["model_id = ? AND operating_system_id = ?", model_id, operating_system_id], :include => [:tasks])
  end

I tried rendering the data as i normally would and then using .each do |f| 我尝试像往常一样渲染数据,然后使用.each do | f | in the view layer, but i get: 在视图层,但我得到:

undefined method `each' for #<Procedure:0x2b879be1db30>

Original Question: 原始问题:

I am creating a rails application to track processes we perform. 我正在创建一个Rails应用程序来跟踪我们执行的流程。 When a new instance of a process is created I want to automatically create rows for all the tasks that will need to be performed. 创建流程的新实例时,我想为所有需要执行的任务自动创建行。 tables: 表格:

  • decommissions 退役
  • models 楷模
  • operating_systems 操作系统
  • procedures 程序
  • tasks 任务
  • procedures_tasks procedure_tasks
  • host_tasks host_tasks

procedures -> tasks is many to many through the procedures_tasks join table. 程序->任务通过procedures_tasks连接表有很多对很多。

when you start a new decommissioning process you specify a model and OS, the model and OS specify which procedure you follow, each procedure has a list of tasks available in the join table. 当开始新的停用过程时,您指定模型和OS,模型和OS指定要遵循的过程,每个过程在联接表中都有可用任务列表。 I am wanting to create a entry in host_tasks for each task relevant to the procedure relevant to the decommission being created. 我想在host_tasks中为与正在创建的与解除授权相关的过程相关的每个任务创建一个条目。

I've done my head in over this for days, any suggestions? 我已经做了几天了,有什么建议吗?

class Procedure < ActiveRecord::Base
  has_and_belongs_to_many :tasks
 #has_many :tasks, :through => :procedures_tasks
# has_many :procedures_tasks
  belongs_to :model
  belongs_to :operating_system
  validates_presence_of :name
  validates_presence_of :operating_system_id
  validates_presence_of :model_id

  def self.get_tasks(model_id, operating_system_id)
    find(:first,  :select => 'tasks.id, procedures.id', :conditions => ["model_id = ? AND operating_system_id = ?", model_id, operating_system_id], :include => [:tasks])
  end

end

the get_tasks method will retrieve the tasks associated with the procedure, but I don't know how to manipulate the data pulled from the database in rails, I haven't been able to access the attributes of the returned object through the controller because they haven't been rendered yet? get_tasks方法将检索与该过程关联的任务,但是我不知道如何操作从Rails中从数据库中提取的数据,我无法通过控制器访问返回对象的属性,因为它们没有还没有被渲染?

ideally i would like to be able to format this data so that I only have an array of the task_id's which i can then loop through creating new rows in the appropriate table. 理想情况下,我希望能够格式化此数据,以便仅拥有一个task_id数组,然后可以循环在相应表中创建新行。

It wasn't looping through because I was using the :first option when finding the data. 它没有循环,因为我在查找数据时使用了:first选项。 I changed it to :all which allowed me to .each do |f| 我将其更改为:all。 etc. 等等

Not the best option, but there will only ever be one option anyway, so it won't cause a problem. 这不是最好的选择,但是总会有一个选择,因此不会造成任何问题。

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

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