Ruby/Rails - Models Named with Two Words (Naming Convention Issues)

后端 未结 4 711
青春惊慌失措
青春惊慌失措 2020-12-23 16:44

This is really a question about naming conventions.

I have a model called PromotedEvents

The file is called promoted_events.rb

I created the table wi

相关标签:
4条回答
  • 2020-12-23 17:26

    If it helps, I always think of it like this:

    The model name is singular because it represents a single, specific thing. So, PromotedEvent is a specific promoted event that has a name, date, etc.

    The table name on the other hand is plural. This is because the table stores a collection of these singular items. So, promoted_events.

    In rails, filenames are mostly a matter of convention since ruby has pretty lax rules in this regard, but generally it's class_name.rb. This page might help you get a better overview of what conventions are used where and what is specific to Ruby versus Rails.

    0 讨论(0)
  • 2020-12-23 17:39

    Your class should be singlular.

    Name it PromotedEvent in the file promoted_event.rb

    a = PromotedEvent.new
    
    0 讨论(0)
  • 2020-12-23 17:40

    If you are an extreme rails n00b like me, then you will want to remember to create a class definition for your newly created table and place it in app/models.

    It would go like

    class LargeCat < ActiveRecord::Base
        belongs_to :zoo
    end
    
    0 讨论(0)
  • 2020-12-23 17:46

    Model names are singular and camel case like so pe = PromotedEvent.new()

    the file should be promoted_event.rb

    Controllers are plural

    PromotedEventsController

    constants are ALL_CAPS

    locals are separated_by_underscores_and_lowercase

    table names are plural 'SELECT * FROM promoted_events`

    0 讨论(0)
提交回复
热议问题