0

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>
5
  • Does it give an error on your console? Commented Aug 3, 2012 at 13:47
  • It does now. Ill update what show the error look like Commented Aug 3, 2012 at 14:05
  • Following Railscasts Tutorial I should not add a relationship from customer -> books. But many other people seem to suggest to do so with has_many. What his the best way, How should i create a new item. I am very confused on how ruby acts. Commented Aug 3, 2012 at 14:13
  • If you need to have something like: cust = Customer.find(1), cust.books You need to have in your Customer model a has_many :books relashionship. Commented Aug 3, 2012 at 14:31
  • That would resolve my console issues, but it doesn't resolve the creation of books and book_manager in one form. How could i do this? Commented Aug 3, 2012 at 14:33

1 Answer 1

1

You should use a has_many: through: relationship

class Customer < ActiveRecord::Base
  has_many :book_managers, :dependent => :destroy
  has_many :books, :through => :book_managers
  accepts_nested_attributes_for :book_managers
end

class BookManager < ActiveRecord::Base
  belongs_to :customer
  has_many :books, :dependent => :destroy
end

class Book < ActiveRecord::Base
  has_many :book_manager
  def customer
    book_manager.customer
  end
end

You can read more http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

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.