How to structure a Ruby on Rails model? -
in our app's user model, have:
attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest before_save { self.email = email.downcase } validates :first_name, presence: true, length: { maximum: 50 } validates :last_name, presence: true, length: { maximum: 50 } valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: valid_email_regex }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
now, need add relationship associations model, namely:
has_many :roles, dependent: :destroy has_many :agendas, through: :roles
does matter whether include latter go before or after former, in model?
if so, recommended / preferred / best way?
it doesn't matter, important thing consistent. usual best practice first can declare class' structure, before in operational details. example:
class user < activerecord::base attr_accessor :remember_token, :activation_token, :reset_token has_many :roles, dependent: :destroy has_many :agendas, through: :roles before_save :downcase_email before_create :create_activation_digest before_save { self.email = email.downcase } validates :first_name, presence: true, length: { maximum: 50 } validates :last_name, presence: true, length: { maximum: 50 } valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ validates :email, presence: true, length: { maximum: 255 }, format: { with: valid_email_regex }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true end
again, 1 way things, it's common amongst rails applications.
Comments
Post a Comment