Counting records in an association

Hi,
I would like to know how to count records in an association.
My Models look like this:

// Users
class User < ActiveRecord::Base
has_many :read
has_many :books, :through => :read
end

//Read
class Read < ActiveRecord::Base
belongs_to :user
belongs_to :book
end

//Books
class Book < ActiveRecord::Base
has_many :read
has_many :books, :through => :read
end

In my book_controller I have these queries:
@user = User.find(params[:user_id])
@topics = Topic.find(:all)
@read_by_topic = Book.find(:all, :conditions =>
“topic_id=#{@user.topic_id.to_i}”)
@pending_books = @read_by_topic - @user.books

If I use this code @user.assignments.count it returns the number of
books I’ve read from the join model.

My question is, how do I find out how many of books I still need to
read?
@read_by_topic - @user.books returns an array but I can’t add a count.
If I do it says I get an error that says, can’t convert Fixnum into
Array.

Any help?

What does the assignments model look like?

Not sure what you mean by “@read_by_topic - @user.books returns an array
but I can’t add a count.”

Can you do @pending_books.length maybe?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs