When processing data extracted from DB with rails, it is not possible to use an appropriate method because the stored data is not an array or a hash.
Also, when I can confirm that it is an array, I don't know why using the method used for the array doesn't return the expected result for that method ...
In this case, when using the method to extract the elements of the array, somehow the array is extracted and displayed. Why is this happening?

Error message

I want to extract the value of: namestored in the@membersarray, but with the current code

Grandpa [#<Student id: 1, name: "Grandpa", grade: 3, content: "I live in the mountains", created_at: "2018-08 -31 03:55:30 ", updated_at:" 2018-08-31 03:55:30 ", bukatu_id: 1>]

member.nameseemed to be able to get the value ofname:, but then the result is that the array itself is output

Applicable source code
class BukatusController<ApplicationController
  def index
    @bukatus = Bukatu.all
  def show
    @bukatu = Bukatu.find (params [: id])
    @members = @ bukatu.students
class StudentsController<ApplicationController
  def show
    @student = Student.find (params [: id])
  def new
  def create
    @student = Student.new (student_params)
    if @ student.save
       flash [: notice] = "Registered!"
       redirect_to @student
       flash [: notice] = "Please enter again!"
       redirect_to new_student_path
   def student_params
     params.require (: student) .permit (: name,: grade,: content,: bukatu_id)
class Bukatu<ApplicationRecord
    has_many: students
class Student<ApplicationRecord
    belongs_to: bukatu
    validates: name, presence: true
    validates: grade, presence: true
    validates: content, presence: trueend
<h1>Member List</h1>
<% = @ bukatu.name%><br>
<% = @ members.each do | member |%>
<% = member.name%>
<% end%>
<% console%>

Check what each variable stores inweb_console

=>#<ActiveRecord :: Associations :: CollectionProxy [#<Student id: 1, name: "Grandfather", grade: 3, content: "I live in the mountains", created_at: "2018-08- 31 03:55:30 ", updated_at:" 2018-08-31 03:55:30 ", bukatu_id: 1>]>

If i directly describe the processing fetched in a loop inshow.html.erb, different results will be returned. Since the result of.nameof another model is returned,

>>@ members.name
>>@ members.class
=>Student :: ActiveRecord_Associations_CollectionProxy

Since the mysterious reference destinationActiveRecord_Associations_CollectionProxywas found, theArraymethod was determined not to be used. Because

>>@ members.id
NoMethodError: undefined method `id 'for #<Student :: ActiveRecord_Associations_CollectionProxy: 0x00007fef0611bd98>

The result of setting: idto.idis also different from.nameand an error is generated. So

>>@ members.target
=>[#<Student id: 1, name: "Grandfather", grade: 3, content: "I live in the mountains", created_at: "2018-08-31 03:55:30", updated_at: "2018-08-31 03:55:30", bukatu_id: 1>]
>>@ members.target.class

It changed toArrayclass where.targetwas added, so check the contents. It is determined that the stored destination is an array.

>>@ members.target.select {| x | x == name}
NameError: undefined local variable or method `name 'for #<#<Class: 0x00007fef006e9fc8>: 0x00007fef006e7e58>

Trying to extract a value from the correspondingkeyusing theselectmethod, but error

>>@ members.target.assoc (: name)

Trying the same thing using the.assocmethod is an error. Also

>>@ members.target.find (: name)
=>#<Enumerator: [#<Student id: 1, name: "Grandfather", grade: 3, content: "I live in the mountains", created_at: "2018-08-31 03:55: 30 ", updated_at:" 2018-08-31 03:55:30 ", bukatu_id: 1>]: find (: name)>

When using thefindmethod,<Enumerator:appears at the beginning, and eventually thename:value cannot be extracted.

&gt;&gt;@members [: name]
TypeError: no implicit conversion of Symbol into Integer

Assuming that the stored format ishash, an unexpected error occurs if the symbol cannot be converted to a number even if the value is extracted.

Supplemental information (FW/tool version etc.)


  • Answer # 1

    The output of the array itself is

    <% = @ members.each do | member |%>

    The reason is that=of the output is written in the line.

    Now, what I tried. I think it ’s a great way to explore.
    I will look at it one by one.


    show.html.erbreturns a different result if you directly describe the processing that is taken out in a loop.

    This is the difference because=is output as a string.


    Also stored: The result of setting id to .id is different from .name and gives an error.

    @ members.nameis not an attributename, but a method Module # name is called that returns the name of the Module/Class itself (not checked) So sorry if it is an error).
    @ members.idis an error because there is no corresponding method.


    Attempting to extract a value from the correspondingkeyusing theselectmethod, but error

    Enumerable # selectreturns an array consisting only of elements whose block result istrue, so it is not suitable for extracting values.
    The reason for the error is thatnamein the block is undefined.

      It is an error to try the same thing using the


    Array # associs a method that inspects [0] of the elements in the array in order, and returns the element when it finds the same value as the argument.

    [[: apple, "apple"], [: banana, "banana"], [: orange, "orange"]]. assoc (: banana)
    # =>[: banana, "banana"]

    Whenfindmethod is used,<Enumerator:appears at the beginning

    Enumerable # findexamines the element and returns the first element whose block result is true.
    If no block is given, theEnumeratorbefore inspecting the element is returned.
    The argument is the one that iscallwhen there is no element for which the block result is true.


    Unexpected error if symbol cannot be converted to a number.

    Array # []is called throughrecordsindelegate.
    Synonymous with@ members.records [: name].
    Array # []implicitly converts the argument to a number (strict conversionto_int).
    I think this error occurred because: nameis not an object that can beto_int.

    How to extractnamefrom@members,

    @ members.pluck (: name)

    Isn't it good to do it?
    Reference: Merits and Demerits of pluck (to avoid unnecessary DB access) | jkr_2255