Hey, I am putting quite alot of methods in my models such as User, for things such as: class User < ActiveRecord::Base ..... # Count all new messagebox messages def count_new_mbox count_new_privmsgs + count_new_comments + count_new_requests end # Count new private messages def count_new_privmsgs privmsgs.find(:all, :conditions => "is_unread = 1").size end # Count number of new comments for the user def count_new_comments user_comments.find(:all, :conditions => "is_unread = 1").size end end is this the best way to go about implementing these methods? cos it seems to me that im going to have HEAPS of these methods in my model, for even my finds. eg. find_recent_comments(n), find_recent_privmsgs(n). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You could do a :counter_cache on the has_many relationship. This way it stores it as a field on your user record rather than working it out on the fly whenever it''s required. has_many :unread_messages, :class_name => "Message", :conditions => "unread = 1", :counter_cache => true On Dec 21, 2007 5:16 PM, Chubbs <acetinick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey, > > I am putting quite alot of methods in my models such as User, for > things such as: > > class User < ActiveRecord::Base > ..... > > # Count all new messagebox messages > def count_new_mbox > count_new_privmsgs + count_new_comments + count_new_requests > end > > # Count new private messages > def count_new_privmsgs > privmsgs.find(:all, :conditions => "is_unread = 1").size > end > > # Count number of new comments for the user > def count_new_comments > user_comments.find(:all, :conditions => "is_unread = 1").size > end > end > > is this the best way to go about implementing these methods? cos it > seems to me that im going to have HEAPS of these methods in my model, > for even my finds. eg. find_recent_comments(n), > find_recent_privmsgs(n). > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ahh wow, thanks didnt know about that. so will that return an array of Message objects ? On Dec 21, 5:52 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You could do a :counter_cache on the has_many relationship. This way it > stores it as a field on your user record rather than working it out on the > fly whenever it''s required. > > has_many :unread_messages, :class_name => "Message", :conditions => "unread > = 1", :counter_cache => true > > On Dec 21, 2007 5:16 PM, Chubbs <acetin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hey, > > > I am putting quite alot of methods in my models such as User, for > > things such as: > > > class User < ActiveRecord::Base > > ..... > > > # Count all new messagebox messages > > def count_new_mbox > > count_new_privmsgs + count_new_comments + count_new_requests > > end > > > # Count new private messages > > def count_new_privmsgs > > privmsgs.find(:all, :conditions => "is_unread = 1").size > > end > > > # Count number of new comments for the user > > def count_new_comments > > user_comments.find(:all, :conditions => "is_unread = 1").size > > end > > end > > > is this the best way to go about implementing these methods? cos it > > seems to me that im going to have HEAPS of these methods in my model, > > for even my finds. eg. find_recent_comments(n), > > find_recent_privmsgs(n). > > -- > Ryan Bigghttp://www.frozenplague.net--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yes, as only has_many does. On Dec 21, 2007 5:27 PM, Chubbs <acetinick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ahh wow, thanks didnt know about that. so will that return an array > of Message objects ? > > > On Dec 21, 5:52 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > You could do a :counter_cache on the has_many relationship. This way it > > stores it as a field on your user record rather than working it out on > the > > fly whenever it''s required. > > > > has_many :unread_messages, :class_name => "Message", :conditions => > "unread > > = 1", :counter_cache => true > > > > On Dec 21, 2007 5:16 PM, Chubbs <acetin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > Hey, > > > > > I am putting quite alot of methods in my models such as User, for > > > things such as: > > > > > class User < ActiveRecord::Base > > > ..... > > > > > # Count all new messagebox messages > > > def count_new_mbox > > > count_new_privmsgs + count_new_comments + count_new_requests > > > end > > > > > # Count new private messages > > > def count_new_privmsgs > > > privmsgs.find(:all, :conditions => "is_unread = 1").size > > > end > > > > > # Count number of new comments for the user > > > def count_new_comments > > > user_comments.find(:all, :conditions => "is_unread = 1").size > > > end > > > end > > > > > is this the best way to go about implementing these methods? cos it > > > seems to me that im going to have HEAPS of these methods in my model, > > > for even my finds. eg. find_recent_comments(n), > > > find_recent_privmsgs(n). > > > > -- > > Ryan Bigghttp://www.frozenplague.net > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Consider good oo programming concepts while coding. The methods related to messages should go into message model. It is unfortunate even plugins like restful authentication violate basics of oo programming. It becomes even more difficult for newbies to write good code. Sent from my iPhone On Dec 20, 2007, at 10:46 PM, Chubbs <acetinick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey, > > I am putting quite alot of methods in my models such as User, for > things such as: > > class User < ActiveRecord::Base > ..... > > # Count all new messagebox messages > def count_new_mbox > count_new_privmsgs + count_new_comments + count_new_requests > end > > # Count new private messages > def count_new_privmsgs > privmsgs.find(:all, :conditions => "is_unread = 1").size > end > > # Count number of new comments for the user > def count_new_comments > user_comments.find(:all, :conditions => "is_unread = 1").size > end > end > > is this the best way to go about implementing these methods? cos it > seems to me that im going to have HEAPS of these methods in my model, > for even my finds. eg. find_recent_comments(n), > find_recent_privmsgs(n). > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
To me it makes more sense to put all the methods relating a User to fetch and retrieve data inside the User. Why would it be good oo programming to put these methods inside my message object? So you mean to put inside message something like : class Message < ActiveRecord::Base def count_unread(user) user.messages.find(:all, :conditions => "is_unread =true").size; end end On Dec 21, 7:31 pm, Bcp <bcpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Consider good oo programming concepts while coding. The methods > related to messages should go into message model. > > It is unfortunate even plugins like restful authentication violate > basics of oo programming. It becomes even more difficult for newbies > to write good code. > > Sent from my iPhone > > On Dec 20, 2007, at 10:46 PM, Chubbs <acetin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hey, > > > I am putting quite alot of methods in my models such as User, for > > things such as: > > > class User < ActiveRecord::Base > > ..... > > > # Count all new messagebox messages > > def count_new_mbox > > count_new_privmsgs + count_new_comments + count_new_requests > > end > > > # Count new private messages > > def count_new_privmsgs > > privmsgs.find(:all, :conditions => "is_unread = 1").size > > end > > > # Count number of new comments for the user > > def count_new_comments > > user_comments.find(:all, :conditions => "is_unread = 1").size > > end > > end > > > is this the best way to go about implementing these methods? cos it > > seems to me that im going to have HEAPS of these methods in my model, > > for even my finds. eg. find_recent_comments(n), > > find_recent_privmsgs(n).--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Dec 21, 2007 10:44 AM, Chubbs <acetinick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So you mean to put inside message something like : > > class Message < ActiveRecord::Base > def count_unread(user) > user.messages.find(:all, :conditions => "is_unread => true").size; > end > endI''m a bit new to Rails, but doesn''t scoping let you do something like user.messages.count_unread (Assuming user has_many messages and message belongs_to user.) Then implement a class method similar to your method above. class Message < ActiveRecord:Base belongs_to => :user def self.count_unread find(:all, :conditions => {:is_unread => true}).count end end AFAIK, all finds on user.messages are scoped as if you had a condition :user_id => user.id, but I haven''t looked at the details. Maybe someone else can confirm or correct me. - Martin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 21 Dec 2007, at 13:12, Martin Svalin wrote:>> class Message < ActiveRecord::Base >> def count_unread(user) >> user.messages.find(:all, :conditions => "is_unread =>> true").size; >> end >> end > > I''m a bit new to Rails, but doesn''t scoping let you do something like > > user.messages.count_unread > > (Assuming user has_many messages and message belongs_to user.) > Then implement a class method similar to your method above. > > class Message < ActiveRecord:Base > belongs_to => :user > def self.count_unread > find(:all, :conditions => {:is_unread => true}).count > end > end > > AFAIK, all finds on user.messages are scoped as if you had a condition > :user_id => user.id, but I haven''t looked at the details. Maybe > someone else can confirm or correct me.Correct, also, the use of .size is a very bad solution, as it will effectively load all the records into memory (as an array of records). The count method will perform an SQL count. Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ok thanks so much guys for your help, ill defiantly go about implementing my methods like that from now on, thanks, chubbs On Dec 21, 11:31 pm, Peter De Berdt <peter.de.be...-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> On 21 Dec 2007, at 13:12, Martin Svalin wrote: > > > > >> class Message < ActiveRecord::Base > >> def count_unread(user) > >> user.messages.find(:all, :conditions => "is_unread => >> true").size; > >> end > >> end > > > I''m a bit new to Rails, but doesn''t scoping let you do something like > > > user.messages.count_unread > > > (Assuming user has_many messages and message belongs_to user.) > > Then implement a class method similar to your method above. > > > class Message < ActiveRecord:Base > > belongs_to => :user > > def self.count_unread > > find(:all, :conditions => {:is_unread => true}).count > > end > > end > > > AFAIK, all finds on user.messages are scoped as if you had a condition > > :user_id => user.id, but I haven''t looked at the details. Maybe > > someone else can confirm or correct me. > > Correct, also, the use of .size is a very bad solution, as it will > effectively load all the records into memory (as an array of > records). The count method will perform an SQL count. > > Best regards > > Peter De Berdt--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---