简体   繁体   中英

Ruby on Rails calling methods of objects

There is something I don't understand about ruby.

@items.each do |item| 
  item.column
end

will work and return me the value for that column in rails. but

item = @items[some_item_id]
item.column

will give me a method not found exception for nil. Both times I get the object but only with the first I can access the rails data methodes.

What do those dashes |...| do and how do I access such methods?

这是因为在第二种情况下你传递了错误的some_item_id ,所以@items[some_item_id]找不到合适的item并返回nil

@items.each do |item| 
 item.column
end

In above block ruby iterate over records that fetched from database So you are not getting any nil error. But in second case you are trying to fetch record from their index in @items . It's failing because some of your records get deleted from database. OR it will fail on last record only because index start with 0 and id of a table from 1. So miss match occurs when we call from index.

This gathers all elements inside @items for passage into a block:

@items.each

#each will work on hashes, arrays, and other enumerables.


This selects a specific element inside @items :

@items[some_item_id]

The square brackets are a method (named #[] ) for element reference in both hashes and arrays. If you get a MethodNotFound error, it means @items is not a hash or array and doesn't have a method named #[] .

If @items is a collection of ActiveRecord objects and you want to select one by ID, use:

@items.find(some_item_id)

You need to appreciate how ActiveRecord works with Rails


ActiveRecord

What you're doing when you call @items.each do ... is looping through the collection of Item objects in the @items variable. As ruby deals with looping with a code block .

For lack of a better explanation, code blocks are essentially small in-line methods, which have their own local variables, and can be called when using loops (much like most other programming languages). The most important thing to note here is the data you have in the @items variable.

--

Data

Rails is an object-orientated framework (by virtue of being built on Ruby). This means that your @instance variables will basically treated as objects , which means you have to be very careful when populating them with the respective data:

在此输入图像描述

The bottom line is, as Marek stipulated, you cannot set @items = Item.all , and then call an item by its id from an array. The object you're dealing with isn't an array - it's an object, and as such, you'll have to use the methods available to you to access the data you need.

There are two ways you can access the data you want:

  1. Use the find method
  2. Only look up particular Item records

If you don't mind being hacky, you could use the ActiveRecord .find method:

<% item = @items.find [your_item_id] %>
<%= item.column %>

This will give you the ability to look up the particular item within the collection of @items you have.

Alternatively, you'll want to do things "properly" by setting the relevant @item object in your controller:

#app/controllers/items_controller.rb
class ItemsController < ApplicationController
   def show
      @item = Item.find "your_id"
   end
end

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