In Ruby's Hash, the order of its elements is arbitrary. When it is inspect-ed, (as in the output of rails console), the method inspect
just uses each_pair
or something similar, and so the order can be arbitrary. In practice, the order for Hash#each_pair
usually seems to follow the order each Hash element is created; however there is no guarantee! Also, how Rails (or ActiveRecord) creates those instances is another issue (I guess the alphabetical order you see originates how Rails created them; it may have something to do with how they are stored in the DB). Anyway, the bottom line is you can not control how they are stored, as long as a Hash is used. But you can tweak how they are inspect-ed.
The following strategy is to redefine ApplicationRecord#inspect
for any of its child classes so the inspect
method of any Models are modified but of none of any other classes.
Put the following code in whatever file in your app/
directory (e.g., an obvious filename is /app/models/application_record.rb
):
class ApplicationRecord
alias_method :inspect_orig, :inspect if ! self.method_defined?(:inspect_orig)
def inspect
klass = self.class
if klass == ApplicationRecord
return inspect_orig
end
attr_list = attribute_names.sort.map { |name| "#{name}: "+attributes[name].inspect }.join ', '
sprintf('<%s:0x%s %s>', klass.name, object_id.to_s(16), attr_list)
end
end
Or, instead you can run it every time you open rails console
if you want to activate it only temporarily during the session.
Note the name of the parent class of ApplicationRecord
may be added as follows (in the case of Rails 5.2; but I don't think inclusion of the parent class is mandatory, for the class ApplicationRecord
should be defined before the file is read).
class ApplicationRecord < ActiveRecord::Base
I notice your rails console outputs look slightly different from my Rails 5.2.1, maybe due to the difference in the versions(?). In the code snippet above, I have tried to mimic the outputs you seem to get. Anyway adjust it to your liking; now you have a total control on it!
EDIT1:
The description above applies to Rails 5. In Rails 4, the class name should be
class ActiveRecord::Base
EDIT2:
In the code snippet above, it shows #object_id
(n.b., it is not displayed in Rails 5.2). However, what #inspect
shows for Object instances is different from #object_id
, as explained in detail in this answer. If you prefer to display it, a way is this (the left side value is the format for sprintf
):
'0x%016x' % (object_id << 1)