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 basics
Rails Review 1
Java tips, tips
Rails API
Rails migration
[Rails] first_or_initialize
rails tutorial
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
[Rails] devise
rails tutorial
rails tutorial
rails method
rails tutorial
Java Tips
Rails Review 2
Rails CSV basics
Rails6 jQuery introduced
About Rails routing
Rails Routing Basics
JAX-RS (Jersey) Tips
Rails database basics
Rails access restrictions
Java code TIPS
Rails and FormData
rails tutorial Chapter 6
Rails render redirect_to
[Rails] Favorite feature
[Rails] Naming convention
Rails Logger Basics
[Rails] Introducing jquery
tips for java.nio.file.Path
Rails Flash Message
Tailwind on Rails
rails tutorial Chapter 1
Rails delegate method
[Rails 6] cocoon_ introduction
[Rails] Introduce Carrierwave
Rails tutorial memorandum 1
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
rails tutorial Chapter 10
[Rails] Extend UrlHelper
Rails learning day 2
rails tutorial Chapter 9