Skip to content

Instantly share code, notes, and snippets.

@avit
Created August 13, 2012 04:04
Show Gist options
  • Save avit/3336857 to your computer and use it in GitHub Desktop.
Save avit/3336857 to your computer and use it in GitHub Desktop.
Testing associations
class Army < AR:B
has_many :robots
has_many :weapons
end
class Robot < AR:B
belongs_to: :army
has_one :weapon
end
# A weapon MAY belong to a robot if the army assigns it to him.
# * Is it better to validate this association by _id or model?
# * How to test? Can I inject a stub into army.robots relation to return an array?
class Weapon < AR:B
belongs_to :army
belongs_to :robot
validates :army, presence: true
validates :robot, inclusion: {in: proc { |w| w.army.robots }}, allow_nil: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment