With an active record, we can access a value of type
user = User.find(1) user.name #=> 'John'
or
user[:name] #=> 'John'
I'm just wondering when to use, or is there any best practice out there? Personally, I would prefer method access, because I feel like it is more like a ruby path. However, when I see the code by others, I come across hash access.
method access
hash access
The Rails convention is to use ActiveRecord :: AttributeMethods :: Read #read_attribute (dot notation), and not its alias ActiveRecord :: AttributeMethods #[] , which:
read_attribute
[]
, attr_name typecast (, "2004-12-12" , Date.new(2004, 12, 12)). ActiveModel::MissingAttributeError, .
attr_name
"2004-12-12"
Date.new(2004, 12, 12)
ActiveModel::MissingAttributeError
, .
name, , , - , :
name
def name "Awesome #{super}!" end
, , , , [], . [], . .
, , , , , " ", . , , . [] .
[] , , , "" , .
Source: https://habr.com/ru/post/1661709/More articles:(null) в исключении командной строки в saveAsTextFile() на Pyspark - jupyter-notebookConvert XML image path to image using jQuery - jqueryОшибка при развертывании angular 2 приложения на героку - angularStartup time to run the first unit test in VS2015 - c #wcf with https without iis - c #Django tool with login and password that hides all pages of a website - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1661711/angular-2-functions-to-be-used-across-all-components&usg=ALkJrhiPIh5IupoJvV_9GowQKSEJ473WvQMaterial-UI with reactjs: how to use or implement breadcrumbs - reactjsk-fold cross validation in dplyr? - rThe advantage of MVP in android - androidAll Articles