的Ruby / Rails - 记录添加到一个对象,每个循环迭代/对象VS阵列 [英] Ruby/Rails - Add records to an object with each loop iteration / Object vs Arrays
问题描述
我试图弄清楚如何将记录添加到现有的对象为循环的每个迭代。我有一个很难发现的对象和数组之间的区别。
我有这种
@events = Event.find(1)
@loops = Choices.find(:全部:上限=→5)#so环路选择模型的5个实例
在@loops循环
@events = Event.find(:全部:条件=> [?event.id =,loop.event_id])
结束
我想一个新的事件添加到现有@events对象,不管以何种的循环变量是编号。但是,(=)运算符仅创建@events的新实例对象。
我试过(+ =)和(小于≤)作为运营商,但得到的错误
你有一个零对象,当你没想到吧!你可能期望阵列的一个实例。在评估为零时发生错误
我试图创建一个数组
事件= []
事件<< Event.find(1)
@loops = Choices.find(:全部:上限=→5)#so环路选择模型的5个实例
在@loops循环
事件<< Event.find(:全部:条件=>event.id =?,loop.event_id])
结束
但我不知道如何调用数组在视图中的属性。
使用的对象,我能够做的视图中创建一个循环,并调用该对象的所有属性,以及...
<表>
<%为@events%GT事件;
&所述; TR>
&所述; TD>&下;%=的link_to event.title,事件%GT;&下; / TD>
&所述; TD>&下;%= event.start_date%GT;&下; / TD>
&所述; TD>&下;%= event.price%GT;&下; / TD>
< / TR>
<%结束%GT;
< /表>
我怎么可以这样使用数组设置?
所以,问题是
1),请告诉我数组和对象之间的区别?结果
2)有没有一种方法来加入到每次迭代现有对象?结果
3)如果我使用一个数组,有没有办法来调用视图中为每个阵列记录的属性?
@events = []
@events<< Event.find(1)
@loops = Choices.find(:全部:上限=大于5)
在@loops循环
ES = Event.find(:全部:条件=> [?event.id =,loop.event_id])
为电子商务在ES
@events<< Ë
结束
结束
I'm trying to figure out how to add records to an existing object for each iteration of a loop. I'm having a hard time discovering the difference between an object and an array.
I have this
@events = Event.find(1)
@loops = Choices.find(:all, :limit => 5) #so loop for 5 instances of choice model
for loop in @loops
@events = Event.find(:all,:conditions => ["event.id = ?", loop.event_id ])
end
I'm trying to add a new events to the existing @events object based on the id of whatever the loop variable is. But the ( = ) operator just creates a new instance of the @events object.
I tried ( += ) and ( << ) as operators but got the error
"You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil"
I tried created an array
events = []
events << Event.find(1)
@loops = Choices.find(:all, :limit => 5) #so loop for 5 instances of choice model
for loop in @loops
events << Event.find(:all,:conditions => ["event.id = ?", loop.event_id ])
end
But I dont know how to call that arrays attributes within the view
With objects I was able do create a loop within the view and call all the attributes of that object as well...
<table>
<% for event in @events %>
<tr>
<td><%= link_to event.title, event %></td>
<td><%= event.start_date %></td>
<td><%= event.price %></td>
</tr>
<% end %>
</table>
How could i do this with an array set?
So the questions are
1) Whats the difference between arrays and objects?
2) Is there a way to add into the existing object for each iteration?
3) If I use an array, is there a way to call the attributes for each array record within the view?
@events = []
@events << Event.find(1)
@loops = Choices.find(:all, :limit => 5)
for loop in @loops
es = Event.find(:all, :conditions => ["event.id = ?", loop.event_id])
for e in es
@events << e
end
end
这篇关于的Ruby / Rails - 记录添加到一个对象,每个循环迭代/对象VS阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!