Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:24:13+00:00 2026-06-13T19:24:13+00:00

I have a Customer model that has_many polymorphic Address objects, like so: Customer Model:

  • 0

I have a Customer model that has_many polymorphic Address objects, like so:

Customer Model:

class Customer < ActiveRecord::Base
  has_many :mailing_addresses, :as => :addressable, :class_name => 'Address', dependent => :destroy
  accepts_nested_attributes_for :mailing_addresses


  validates :mailing_addresses, :presence => true
  validates_associated :mailing_addresses
end

Address Model:

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true

  validate  :validate_quota
  private
  def validate_quota
    case addressable_type
  when "Customer"
    customer = Customer.find(addressable_id)
    if customer.mailing_addresses.size >= 3
      puts "Adding too many records"
      errors.add(:addressable, "Too many records")
    end
  end
end

I am also using RSpec to test that the quota constraint is respected. So this spec passes, for example:

it "observes quota limit" do
  customer = FactoryGirl.create(:customer, :number_of_mailing_addresses => 3)
  expect {
    address = FactoryGirl.create(:mailing_address, :addressable => customer)
  }.to raise_error
  customer.mailing_addresses.count.should eq(3)
end

Which is good. However, this fails horribly:

it "fails add if already 3 addresses" do
  customer = FactoryGirl.create(:customer, :number_of_mailing_addresses => 3)
  expect {
    customer.mailing_addresses.create( FactoryGirl.attributes_for(:mailing_address).except(:addressable) )            
  }.to raise_error
end

I can even see in the spec output that the Address validation is failing, but for some reason it doesn’t raise an error in “customer.mailing_addresses.create()”, nor does that failed validation prevent the 4th Address model from saving to the database.

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:24:15+00:00Added an answer on June 13, 2026 at 7:24 pm

    Ah, I finally figured it out! I needed to change create to create! in my last spec in order to make the failing validation actually throw an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Customers model: class Customer < ActiveRecord::Base has_many :phone_numbers end and a
I have a customer model that has_many events When I go to the customer
I have Customer, Event and Address objects in my data model. Both Customer and
I have a model called Customer in google app engine with python: class Customer(db.Model):
I have two models: class Customer(models.Model): (...) class CustomerMemo(models.Model): (...) customer = models.ForeignKey(Customer) text
I have my two class in two separate files, customer and address. Address is
I have some models and have views code that look like the following class
I have 3 objects in my model thus: Customer 1-----* Region 1-----* Branch To
Here are my models: class Project < ActiveRecord::Base has_many :gs_collectors, dependent: :destroy class GsCollector
I have a Job model that belongs_to three users (customer, employee, and qa). The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.