tanya
tanya

Reputation: 2985

find :all -- IN condition

I have 2 models in my rails application: Book and Editions. A book can have many editions.

I need to get all the 'editions' of the 'books' written by a specific author.

Find all books by a specific author

 @saved_books = Book.find :all, :conditions => ["aut_fk = ?",@aut.id ] 

@saved_books contains an array of books written by a specific author.

My question is how do i get the editions of the books written by a specific author.

@editions =  Edition.find(:all, :conditions => [ "book_fk IN (?)", @saved_book.id]) //this code is not correct

Any suggestion is most appreciated.

Upvotes: 0

Views: 258

Answers (2)

tanya
tanya

Reputation: 2985

I am using the code below and it seems to do the job. Hope it might be of help to somebody else

@edition =  Edition.find(:all, :conditions => ["book_fk IN (?)", @saved_books.map{|b| b.id}])

Upvotes: 0

Deleteman
Deleteman

Reputation: 8700

Have you tried?:

@edition =  Edition.where(:book_fk => @saved_book.id])

Edit Is @saved_book.id an array? It doesn't look like it. If @saved_book should be @saved_books, then the code would change to:

@edition =  Edition.where(:book_fk => @saved_books.map {|b| b.id}])

I think...

Upvotes: 1

Related Questions