I am just not to sure how to build a nested form. I have followed Ryan Railscasts but I am unsure on how to create a new instance in my case.
I have the following models
Customer,
Book Manager, and
Book
The relationship his has follow
Customer
has_many :book_managers, :dependent => :destroy
accepts_nested_attributes_for :book_managers
Book
belongs_to :book_manager
def customer
book_manager.customer
end
Book_Manager
belongs_to :customer
has_many :books, :dependent => :destroy
The form his has follow
<%= form_for @bookmanager do |f| %>
<%= f.fields_for :books, Book.new do |builder| %>
<div>
<%= builder.label :description %><br />
<%= builder.text_area :description, :rows => 3 %>
</div>
<% end %>
<div class="field">
<%= f.label :visible %><br />
<%= f.text_field :visible %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The part that confused me his to create a new instance in the single form. I have a description which belongs to book and I have a isVisible who belongs to book_managers
Here what i am thinking but doesn't seem to be working
@customer = Customer.find(params[:id])
@bookmanager = BookManager.new(params[:bookmanager])
@book = Book.new(params[:book])
I also try the following
@customer = Customer.find(params[:id])
@bookmanager = @customer.book_managers.build
It doesn't work and not sure how to create the relationship. Any help on it is appreciated!
Here the query i did In rails c
cust = Customer.first
cust.book_managers.build :visible => true
cust.book_managers.first.books.build :description => 'the odyssey'
cust.save!
Seem okay then i did the follow to check
cust = Customer.find 1
cust.books ### This is where the error was given to me
Book.first.customer
The error is
NoMethodError: undefined method `books' for #<Customer:0xad55afc>
cust = Customer.find(1),cust.booksYou need to have in yourCustomermodel ahas_many :booksrelashionship.