开发者

Get values from array returned from model

开发者 https://www.devze.com 2023-02-14 07:44 出处:网络
I have an array that is returned from a model that does not correspond to the view/controller. (i.e Data is from Foo, while I\'m in \'/bar/\'). Therefore I cannot access them via the conventional

I have an array that is returned from a model that does not correspond to the view/controller. (i.e Data is from Foo, while I'm in '/bar/'). Therefore I cannot access them via the conventional

params[:someItem]

So I am trying to ext开发者_运维问答ract values like this

someVariable = @array[0]

However I get a scrambled mess:

#<Promotion:0x3b74140>

Seeing as that the value I want is an int, I tried calling .to_i, which threw a No Such Method error. Calling to_int gave the same result.

Question: How do I get this value out of the array? And as an aside, why do .to_i and .to_int not work??


As per my understanding, You are trying to access Array of model objects. When you write

someVariable = @array[0]

then it will give you the first model object from @array. If you want to access object values then you can use like this

modelObj = @array[0]
someVariable = modelObj.my_attribue_name
0

精彩评论

暂无评论...
验证码 换一张
取 消