[RUBY] Pitfalls of Active Hash

Overview

This time, I was addicted to the pitfall when calling the data stored in Active Hash for product editing, so I will share it.

I tried various searches in qiita, but I couldn't solve it and got swamped ...

Implementation details

Implement the function to edit with the edit action for the item that has been listed by registering all the data once.

The point is that when you move to the edit screen, the originally registered data is registered without being reset.

There is no problem if the call description of the view file and the association of the model can be connected well, but I made an error firmly.

Solution

Let me start with the solution first.

When implementing a function like this, the column name of the data in Active Hash is xxx_id Let's say.

For example, the category and the prefecture of the delivery source are fixed data, so describe them in Active Hash. Here, when associating with the item column, let's firmly set it as category_id or prefecture_id.

On the contrary, when forming an association with a model, It looks like belongs_to_active_hash: category, and you don't need to write _id.

The code is summarized below, so I hope you understand it there.

code

models/item.rb


---abridgement---
extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :category
  belongs_to_active_hash :condition
  belongs_to_active_hash :postage_type
  belongs_to_active_hash :prefecture
  belongs_to_active_hash :preparation_days
---abridgement---

ruby:views/edit.html.erb


<div class="items-detail">
      <div class="weight-bold-text">Product details</div>
      <div class="form">
        <div class="weight-bold-text">
Category
          <span class="indispensable">Mandatory</span>
        </div>
        <%= f.collection_select(:category_id, Category.all, :id, :name, {}, {class:"select-box", id:"item-category"}) %>
        <div class="weight-bold-text">
Product condition
          <span class="indispensable">Mandatory</span>
        </div>
        <%= f.collection_select(:condition_id, Condition.all, :id, :name, {}, {class:"select-box", id:"item-sales-status"}) %>
      </div>
    </div>

The migration file is as follows. Please note that the column name has _id.

○○_create_items.rb



class CreateItems < ActiveRecord::Migration[6.0]
  def change
    create_table :products do |t|
      t.string :name, null: false
      t.text :explanation, null: false
      t.integer :category_id, null: false
      t.integer :condition_id, null:false
      t.integer :postage_type_id, null:false
      t.integer :prefecture_id, null:false
      t.integer :preparation_days_id, null:false
      t.integer :value, null:false
      t.references :user, foreign_key: true
      t.timestamps
    end
  end
end

Finally

I was swamped without realizing that the column name had to have _id. You can understand this theory by playing with the console, but I hope it helps the same beginner who is still not good at error resolution and is in trouble.

Recommended Posts

Pitfalls of Active Hash
Utilization of Active Hash
[Spring] Pitfalls of BeanUtils.copyProperties
[Rails] About active hash
[Rails] Introducing Active Hash
pitfalls of nextInt () → nextLine ()
Subtle pitfalls of lombok
Table design (using Active Hash)
gem active_hash About active hash
Pitfalls of WebTarget.queryParam () in JAX-RS
About the behavior of ruby Hash # ==
hash
Hash
Extraction of "ruby" double hash * Review
[Java] Reference / update of Active Directory
I tried to explain Active Hash