1

I have two models:- a) A b) B

model A looks like this:-

class A < ActiveRecord::Base

has_many :bs ,:dependent => :destroy   
validates :name, :presence=>true
accepts_nested_attributes_for :bs
attr_accessible :bs_attributes

end

model B looks like this

class PlanDay < ActiveRecord::Base

belongs_to :a
attr_accessible :a_attributes

end

In my view I have used form_for and fields_for, but I am confused in the parameter that I am getting:-

 Parameters: {"a"=>{"name"=>"swsdw" , "bs_attributes"=>{"0"=>{"friday"=>"true", "wednesday"=>"", "tuesday"=>"", "sunday"=>"true", "saturday"=>"true", "monday"=>"", "thursday"=>""}}

What is this 0 I am getting in bs_attributes? Is this the reason that I am not able to save the values of my days in 'bs' table?

I am saving this as follow:- @a = A.new(params[:a])

and this is saving my both the models are getting saved , but the all the days that are present in parameters are getting saved with null.

1 Answer 1

2

A has a has_many relationship with B. So, the bs_attributes will actually be an array containing multiple Bs. So, 0 means the first B. If you had passed two Bs, then there would have been 0 as above and an additional hash beginning with 1.

I dont think the 0 is causing B to have null values. And 0 is not causing a problem because you are able to save multiple bs when saving A.

You stated "I am saving this as follow:- @a = A.new(params[:a])". Did you not save @a or you forgot to include it in your question ? Add @a.save! after that. The bang(!) will show you the failed validations if @a is not saved.

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.