Skip to content

Instantly share code, notes, and snippets.

@masterkain
Created September 6, 2008 16:18
Show Gist options
  • Save masterkain/9170 to your computer and use it in GitHub Desktop.
Save masterkain/9170 to your computer and use it in GitHub Desktop.
class Report < ActiveRecord::Base
has_many :report_reasons
has_many :reasons, :through => :report_reasons
validates_associated :reasons
end
class Reason < ActiveRecord::Base
has_many :report_reasons
has_many :reports, :through => :report_reasons
validates_presence_of :content
validates_length_of :content, :within => 3..100
end
## tests (rspec)
@one_invalid_reason_attribute = {
:document_id => 1,
:when => Time.now.utc,
:reason_attributes => [
{ :content => "mr" }
]
}
it "should not be created with one invalid associated reason attribute" do
@report = Report.new(@one_invalid_reason_attribute)
lambda {@report.save!}.should raise_error(ActiveRecord::RecordInvalid)
Reason.count.should == 0
# There @report will have one reason, but an invalid flagged one.
@report.should have(1).reasons
# Rails bug? @errors={"reasons"=>["is invalid", "is invalid"]}
@report.should have(1).errors_on(:reasons)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment