>Not really.  I still want all bob.articles to return ALL of bob's
>articles, but "bob.articles.published" should return only his
>published articles.

This will return all of Bob's articles:  def articles = 
Article.findAllByAuthor("Bob")

This will return only Bob's published articles:  def articles = 
Article.findAllByAuthor("Bob")AndPublished("True")

>And, to take it a step further than a simple property and illustrate
>ActiveRecord's ability to define any method, I would also like a
>about_paris_hilton method defined on the articles association, so I
>could say "bob.articles.about_paris_hilton"

Given this class

class Person {
    String name
    def hasMany = [articles:Article]  
}

You can use Hibernate's Criteria Builder:

def c = Person.createCriteria()
def results = c.list{
    articles{
        like('content','Paris Hilton')
    }
}

Bashar




----- Original Message ----
From: Chad Woolley <[EMAIL PROTECTED]>
To: jug-discussion@tucson-jug.org
Sent: Friday, July 11, 2008 3:39:48 PM
Subject: Re: [jug-discussion] Thanks for the presentation Brian!

On Fri, Jul 11, 2008 at 3:16 PM, Bashar Abdul
<[EMAIL PROTECTED]> wrote:
> If I got your questions right:
>
> Suppose you have this class:
>
> class Article{
>     String author
>     boolean published
> }
>
> To get all the articles published by Bob:
>
> def articles = Article.findAllByAuthor("Bob")AndPublished("True")



And, to take it a step further than a simple property and illustrate
ActiveRecord's ability to define any method, I would also like a
about_paris_hilton method defined on the articles association, so I
could say "bob.articles.about_paris_hilton"

In ActiveRecord, this would look something like this (simple and
probably somewhat inaccurate, I'm no ActiveRecord guru):

class Person < ActiveRecord::Base

  has_many articles do
    def published
      ...
    end

    def about_paris_hilton
      proxy_target.select { |article| article.about_paris_hilton? }
    end
  end
end

With the new named_scope feature (contributed to Rails by one of my
colleagues), this is even easier: http://railscasts.com/episodes/108

Anyway, the point is that the associations themselves are objects
which you can extend with methods and functionality.  You don't have
to implement these helper methods directly on the model class.  This
allows you to make a very expressive and english-like API for your
model layer.

-- Chad

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to