[Rough explanation] Causes and remedies for not being able to obtain the name from the ActiveHash model

__ "I want to get the name from the ActiveHash model, but I get an undefined error and can't get it ..." __

For such people, I will explain how to get the ActiveHash name forcibly and why you can not get the name in the first place.

If you are new to programming, we would appreciate it if you could refer to it. (For those of you who know why you get an error, this article is useless. I'm sorry.)

Purpose of this article

・ Review how to get normal values using ActiveHash -Learn how to get the value from the ActiveHash model with the find method · Understand why undefind errors occur

[Premise] ActiveHash Acquisition of normal value

sushi.rb


class Sushi < ActiveHash::Base
  self.data = [
    { id: 1, name: 'fatty tuna' },
    { id: 2, name: 'Medium Toro' },
    { id: 3, name: 'How much' },
    { id: 4, name: 'conger eel' },
    { id: 5, name: 'Engawa' },
    { id: 6, name: 'sea urchin' },
    { id: 7, name: 'The squid' }
  ] 
end

rhtml:lunch.html.erb


<%= @shari.sushi.name %>
<!--@ Shari sushi_If id is "4", "Conger eel" is displayed-->

With such a description, you can get the value of name associated with @ shari.sushi_id from the ActiveHash model. (The description of the model is omitted this time.)

However, under certain conditions __, the name element of ActiveHash cannot be obtained.

undefined method '〇〇' for …

In some cases, an error will occur.

In such a case, I will tell you how to forcibly get the value from the ActiveHash model.

Part 1: Get and display using find

rhtml:lunch.html.erb


<%= Sushi.find(@shari.neta_id).name %>

"I got an error, but I want to write an Active Hash model and use it ..."

The recommended method is to use the __find method to forcibly get __.

(1) Pass the id you want to get to the argument of the find method and search for @ shari.neta_id from the Sushi model (ActiveHash). (2) I want the name of the acquired id, so I will add .name at the end. ③ From the Sushi model, you can get the name element that matches @ shari.neta_id.

It's a bit brute force, but you can use this method to get the specified name element. However, be careful when using it, as the code tends to be verbose.

Part 2: Get and display with form.select and Case

rhtml:new.html.erb


<%= form.select :neta_id,[["Horse mackerel",1],["Kohada",2],["Redthroat seaperch",3],["Tilefish",4]] %>

rhtml:lunch.html.erb


<% case @shari.neta_id %>
  <% when 1 then %>
     <p>Horse mackerel</p>
  <% when 2 then %>
     <p>Kohada</p>
  <% when 3 then %>
     <p>Redthroat seaperch</p>
  <% when 4 then %>
     <p>Tilefish</p>
<% end %>

"The amount of description is small, and it doesn't have to be ActiveHash ..."

For those who say, by describing the pull-down part in form.select and the display part in the case statement, you can create a pseudo ActiveHash-like expression.

However, this method is recommended to be used only when there are few choices, as the number of choices increases and the amount of description becomes huge, which may reduce the readability of the code.

Why can't I get it in the first place?

In conclusion, if __ "The model name of the ActiveHash model and the column name do not match" __, the name element cannot be obtained in the canonical way.

If the column name is sushi_id (integer type), the ActiveHash model name must also be Sushi.rb. In case of (1) above, even if you try to get the value from Sushi.rb in the form of neta.name, you cannot get it.

When creating an ActiveHash model to create pull-downs and checkboxes, make sure the model name and column name are the same.

Summary

(1) ActiveHash cannot get the name element with .name unless the model name and column name are present. (2) If you want to get the value from the model of another name, you can get the name element by using find. (3) If the description itself (choice) is small, there is also a method of expressing it by using form.select and when.

Thank you for watching until the end. Let's continue to study hard!

Recommended Posts

[Rough explanation] Causes and remedies for not being able to obtain the name from the ActiveHash model
I was addicted to not being able to connect to AWS-S3 from the Docker container
[Rails] The cause of not being able to post was in form_with
Get the column name from the Model instance
Workaround for GMO VPS (CentOS8.1) not being able to start after yum update
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation