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

  • Home
  • SEARCH
  • 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 8829843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:55:40+00:00 2026-06-14T07:55:40+00:00

I have been struggling on this for a couple days. I have this model:

  • 0

I have been struggling on this for a couple days. I have this model:

class BusinessEntity < ActiveRecord::Base

  has_many :business_locations
  accepts_nested_attributes_for :business_locations, :allow_destroy => true,
        :reject_if => proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }

 after_initialize :build_child

  ....

  def build_child
    self.business_locations.build if self.business_locations.empty?    
  end

business_entites.rb (factory)

FactoryGirl.define do
  factory :business_entity do
    name "DaveHahnDev"
    association :company, :factory => :company
    association :default_currency, :factory => :currency

    factory :business_entity_with_locations do
      after(:build) do |business_entity|
        business_entity.class.skip_callback(:create, :after, :set_primary_business_info)
        business_entity.business_locations << FactoryGirl.build(:business_location)
      end
    end
  end

  factory :business_location do
    name "Main Office"
    business_entity
    address1 "139 fittons road west"
    address2 "a different address"
    city { Faker::Address.city }
    province "Ontario"
    country "Canada"
    postal_code "L3V3V3"

  end
end

Now when I call FactoryGirl.create(:business_entity) in a spec I get valdation error on business_locations have blank attributes. This is the child initialized by the after_initialize callback. I thought the reject_if would take care of this, as it does if you use the application from a browser. If i add:

  before_validation :remove_blank_children

  def remove_blank_children
    self.business_locations.each do |bl|
        bl.mark_for_destruction if bl.attributes.all? {|k,v| v.blank?}
    end
  end

everything will pass fine, but I feel like I should not need to do this.

Is it possible I am testing this wrong, or is it bad practice to build children in the models.

Any thoughts will be a great help.

  • 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-14T07:55:42+00:00Added an answer on June 14, 2026 at 7:55 am

    is it bad practice to build children in the models

    Not necessarily, but I’d avoid after_initialize—it is executed on every instantiation of your model, even a straight find.

    I think you’d be better off isolating the cases where you need to add a business_location and doing it explicitly. And as it seems that your business_entity_with_locations factory is doing exactly that, I’m not sure why you need the callback at all.

    As for why the accepts_nested_attributes_for is not working, I believe that is because you are not using it. It expects an attribute hash like:

    { :business_locations => { 0 => { :name => “Sample Name } } }

    to be passed into a method like new. That’s not what you’re doing—you’re calling build on the association without any parameters. So the attribute-setter logic provided by accepts_nested_attributes_for is never invoked.

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

Sidebar

Related Questions

So I've been struggling with this for a couple days now. I have a
I have been struggling with this issue for the past couple days: I have
So I've been struggling with this for the last couple days.. I have music
I have been struggling with a problem for a couple of days now with
I've been struggling with this issue for a couple of days now. I need
I've been struggling for a couple of days now with this problem on QTcpSockets
I have been struggling with this for three days, I want to do a
I've been struggling with this for a couple days now. I'm trying to make
Been struggling with this for a couple days now. Here's the set up -
I've been struggling with this for a couple of weeks now. I have a

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.