1

I have models like this:

class User < ActiveRecord::Base
  has_one :business
end
class Business < ActiveRecord::Base
  belongs_to :user
  has_many : locations
  accepts_nested_attributes_for :locations, :reject_if => lambda { |a| a[:location].blank?} 
  atr_accessible :name, :locations_attributes
  ...
end

class Location < ActiveRecord::Base
  belongs_to :business
  ...
end

when I fill in the address in the form, and post form to the create action of BusinessesController, the log show that the parameters are correct:

... 
"business"=>{"name"=>"sos","locations_attributes"=>{"0"=>{"address"=>"location1"}, "1"=>{"address"=>"location2"}}}
...

In the create action of BusinessesController

# :Post /usres/1/businesses
def create
  @user = User.find(params[:user_id])
  @business = @user.build_business(params[:business])
  if @business.save 
    ...
  else
    ...
  end
end

I check the log and found that @business.save did not insert any information about the locaitons to the database, but only the information about business, but the params[:business] clearly contains the locations hash, so where I was wrong??

0

1 Answer 1

1

I guess where it goes wrong is in the reject_if check,

accepts_nested_attributes_for :locations, 
   :reject_if => lambda { |a| a[:location].blank?}

Where do you have location attribute in locations table? As i understand you should check in the following way

accepts_nested_attributes_for :locations, 
   :reject_if => lambda { |a| a[:address].blank?}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.