aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models/ship.rb
blob: 95172e4d3e720ae9df65b1f9e898e4217b5bb2a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Ship < ActiveRecord::Base
  self.record_timestamps = false

  belongs_to :pirate
  belongs_to :update_only_pirate, :class_name => 'Pirate'
  has_many :parts, :class_name => 'ShipPart'
  has_many :treasures

  accepts_nested_attributes_for :parts, :allow_destroy => true
  accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc(&:empty?)
  accepts_nested_attributes_for :update_only_pirate, :update_only => true

  validates_presence_of :name

  attr_accessor :cancel_save_from_callback
  before_save :cancel_save_callback_method, :if => :cancel_save_from_callback
  def cancel_save_callback_method
    throw(:abort)
  end
end

class ShipWithoutNestedAttributes < ActiveRecord::Base
  self.table_name = "ships"
  has_many :prisoners, inverse_of: :ship, foreign_key: :ship_id
  has_many :parts, class_name: "ShipPart", foreign_key: :ship_id

  validates :name, presence: true
end

class Prisoner < ActiveRecord::Base
  belongs_to :ship, autosave: true, class_name: "ShipWithoutNestedAttributes", inverse_of: :prisoners
end

class FamousShip < ActiveRecord::Base
  self.table_name = 'ships'
  belongs_to :famous_pirate
  validates_presence_of :name, on: :conference
end