Search code examples
ruby-on-railsrubyclassmessage

In Ruby on Rails, where is the class "Message" stored?


I'm trying to understand the syntax and meaning of terms in a Ruby On Rails Codecademy course. It appears that there is a class 'Message' that is a default class (one that I haven't defined).

(EDIT: It turns out that Message isn't a default class. It was defined in ~/app/models/ and I wasn't aware. It was created when I entered the command: rails generate model Message. Any readers of this can probably ignore the rest of my question as it becomes irrelevant. )

For example in the Messages Controller the following method has been defined:

~app/controllers/messages_controller
def new 
@message = Message.new 
end

I understand the above to instruct to create a new instance variable: "@message" of the class: "Message". Before we do this, we usually have to create a class "Message" beforehand, but I don't recall ever doing this, which suggests that Message is a default class.

So my question is, where is the class "Message" defined, and where do these default actions "new", "all", etc come from?

I've searched for "Message" in my rails files and have found the following:

~app/node_modules/@rails/activestorage/README.md:
   64  
   65  ```ruby
   66: class Message < ApplicationRecord
   67    has_many_attached :images
   68  end

Which suggests the class Message inherits from the ApplicationRecord superclass. I've done a search and there are loads of files called "active_record.rb". This raises further questions. How does Ruby know which "active_record.rb" file to refer to?

One of the "active_record.rb" files is saved here:

~app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc1/lib/active_record.rb

Solution

  • The Message class "inherits" from ActiveRecord and is "defined" in app/models/message.rb and generated either by a scaffolding operation as Lucas said, or you can write them manually. Using the scaffolding feature has its advantages especially when dealing with simple CRUD features - it creates the model, migration, spec/test files, then controllers and views. You can customize these further to your liking.

    Here's a guide on the typical Rails application directory structure: https://www.sitepoint.com/a-quick-study-of-the-rails-directory-structure/

    If you're asking where Messages are "stored", they are stored/persisted wherever ActiveRecord is configured to persist data.

    ActiveRecord is the class responsible for persisting (storing) and retrieving your data as you model it using model files such as message.rb located in the app/models directory.

    ActiveRecord can use sqlite, mysql/mariadb, postgres databases and many more using "adapters". In your application you can check config/database.yml to see what's configured and/or change the storage backend.

    The file(s) where classes are "defined" follow a certain naming and location convention with Rails so the loader knows what to look for, eg gems/activerecord-6.0.0.rc1/lib/active_record.rb (ActiveRecord) or app/models/message.rb (Message). You will always be saving your application models under app/models/.