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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
What does the assignments model look like? On Mar 28, 11:01 am, mridelmann <emik...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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...-1OROalxqRjP9KIQWOO/q1A@public.gmane.org_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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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?
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
mridelmann
Sent: Friday, March 28, 2008 8:01 AM
To: Ruby on Rails: Talk
Subject: [Rails] 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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---