モデルクラスでレコードが格納される場所


class Event < ActiveRecord::Base
  belongs_to :owner , class_name: 'User'
  has_many :tickets, dependent: :destroy

  validates :name, length: { maximum: 50 }, presence: true
  validates :place, length: { maximum: 100 }, presence: true
  validates :content, length: { maximum: 2000 }, presence: true
  validates :start_time, presence: true
  validates :end_time, presence: true
  validate :start_time_should_be_before_end_time

  def created_by?(user)
    return false unless user
    owner_id == user.id
  end

  private

  def start_time_should_be_before_end_time
    return unless start_time && end_time

    if start_time >= end_time
      errors.add(:start_time, 'は終了時間よりも前に設定してください')
    end
  end

end

owner_idはミミックメソッド

irb(main):002:0> event = Event.new(name: "test name!")
=> #<Event id: nil, owner_id: nil, name: "test name!", place: nil, start_time: nil, end_time: nil, content: nil, created_at: nil, updated_at: nil>
irb(main):003:0> event.instance_variables
=> [:@attributes, :@column_types_override, :@column_types, :@aggregation_cache, :@association_cache, :@attributes_cache, :@readonly, :@destroyed, :@marked_for_destruction, :@destroyed_by_association, :@new_record, :@txn, :@_start_transaction_state, :@transaction_state, :@reflects_state, :@changed_attributes]
irb(main):004:0> puts event.attributes
{"id"=>nil, "owner_id"=>nil, "name"=>"test name!", "place"=>nil, "start_time"=>nil, "end_time"=>nil, "content"=>nil, "created_at"=>nil, "updated_at"=>nil}
=> nil