Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created June 24, 2012 10:18
Show Gist options
  • Select an option

  • Save jeffrafter/2982727 to your computer and use it in GitHub Desktop.

Select an option

Save jeffrafter/2982727 to your computer and use it in GitHub Desktop.
class Center < ActiveRecord::Base
belongs_to :thing
validates :thing_id, :presence => true
end
class Thing < ActiveRecord::Base
has_many :centers
has_one :default_center, :class_name => 'Center', :conditions => ['centers.default = ?', true]
accepts_nested_attributes_for :default_center
end
describe "default_centers" do
fit "should create a default center with nested attributes" do
params = HashWithIndifferentAccess.new({
"thing"=>{
"default_center_attributes"=>{
"name"=>"Very very good sample name",
"default" => true
}
})
thing = Thing.create!(params[:thing])
thing.should be_valid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment