モデルクラスでレコードが格納される場所
4466 ワード
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
Author And Source
この問題について(モデルクラスでレコードが格納される場所), 我々は、より多くの情報をここで見つけました https://qiita.com/a_ishidaaa/items/08dd512ca6827f1a4d95著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .