On Jun 26, 3:51 pm, Colin Law <clan...@googlemail.com> wrote:
> 2009/6/26 Nick Hoffman <n...@deadorange.com>:
> > In the code below, p.comments.foobar calls #foobar on every comment.
> > Is there a way to call #foobar only on the comments that belong to
> > post "p"?
>
> > --------------------
> > class Comment < ActiveRecord::Base
> >  belongs_to :post
>
> >  def self.foobar; end
> > end
>
> > class Post < ActiveRecord::Base
> >  has_many :comments
> > end
>
> > p = Post.find_by_id 123
> > p.comments.foobar
>
> What you are saying does not make sense to me.  p.comments is an array
> containing the comments that belong to post p.  Therefore
> p.comments.foobar should generate a syntax error as Array does not
> have a method foobar.
>
> To call foobar on each comment belonging to p do
> p.comments.each {|c| c.foobar}
>
> Colin

Hi Colin. I agree: an error should occur when calling
p.comments.foobar . However, no error occurs.

To use your suggestion:
> p.comments.each {|c| c.foobar}
I'll have to change Comment.foobar from a class method to an instance
method.

Thanks for your input!
Nick
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to