Forum: Ruby on Rails Ruby Rails Has_Many fetching data

Posted by Jean-Sébastien D. (jeansbastien_d)
on 2012-08-01 17:55
I am trying to fetch the information from books associated with a
customer but seem that the middle association doesn't works

Here my model

Book
    has_one :book_manager
    has_one :customer, :through => :book_manager

Customer
    has_many :book_managers
    has_many :books, :through => :book_managers

Book_Manager
    belongs_to :customer
    belongs_to :book
The field are has follow

Book          Customer      book_manager
id            id            id
description   email         customer_id
              password      book_id
              first         visible
              last
When fetching the information in my def edit, the following is
successfull

@book = Book.first
@book = Book.last
The following seem to fails

@customer = Customer.find(params[:id])
@book = @customer.books.first
@book = @customer.books.order("created_at DESC").first
Is there something i miss?

I also try to verify by creating an index for book_manager controller
and view and nothing appears, its seems its empty. The way i created the
books was as follow

BookController

def create
@book = current_customer.books.build(params[:book])
    if @book.save
        flash[:success] = "Book Created"
        redirect_to root_url
    else
        render 'customer/edit'
    end
end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.