Created
August 13, 2012 04:04
-
-
Save avit/3336857 to your computer and use it in GitHub Desktop.
Testing associations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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