Rails Tips

table of contents

-[Reason why you can get the value by. (Dot) + column name when accessing the database instance](# Why you can get the value by. (Dot) + column name when accessing the database instance)

Why you can get the value by. (Dot) + column name when accessing the database instance

Because it is an instance (object) created by a class that inherits Active Record, it is possible to get the value using the .method. Since it is not the usual Ruby notation, I want to get the value for the hash object, so if I write it with dots, an error will occur.

How to get the hash value in Ruby

example.rb


hash = {name: 'hoge', email: '[email protected]'}

hash[:name]
=> "hoge"

# .I get an error when I try to get it with
hash.name
NoMethodError (undefined method `name' for {:name=>"hoge", :email=>"[email protected]"}:Hash)
How to get a value in an object created from a class that inherits AR

example.rb


#Assuming that the Hash class has already been defined, Hash.Create an instance with new
hash = Hash.new(name: 'hoge', email: '[email protected]')

hash[:name]
=> "hoge"

hash.name
=> "hoge"

Recommended Posts

Rails Tips
Detailed tips when using Rails
[Rails g.error]
Rails Review 1
Java tips, tips
Rails API
Rails migration
[Rails] first_or_initialize
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
rails tutorial
rails tutorial
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Java Tips
Rails Review 2
Rails6 jQuery introduced
About Rails routing
Add binding.pry (rails)
JAX-RS (Jersey) Tips
Rails database basics
Rails access restrictions
Java code TIPS
Rails and FormData
rails tutorial Chapter 6
Rails tutorial test
[Rails] Favorite feature
Rails Bootstrap introduced
[Rails] Many-to-many creation
[Rails] Naming convention
Rails Credentials Note
Rails Logger Basics
[Rails] Introducing jquery
tips for java.nio.file.Path
[Rails 6] Ranking function
Rails Flash Message
Tailwind on Rails
rails tutorial Chapter 1
[Rails 6] cocoon_ introduction
[Rails] Introduce Carrierwave
Rails tutorial memorandum 1
[rails] Set validation
Rails learning day 3
My Eclipse Tips
Rails tutorial memorandum 2
Rails learning day 4
[WIP] Ruby Tips
Implement Rails pagination
[Rails] Category function
rails tutorial Chapter 7
[Rails] Introducing devise
rails tutorial Chapter 5
Group_by in Rails
Rails follow function