Defining self method and results for find( :all, :select => "DISTINCT()

A first attempt to denine a self method:

In my ProjectItem model I have a field 'book'

In that model there is this method:

def self.find_books     find( :all, :select => "DISTINCT(book)", :order => "book ASC" ) end

In another controller:

@books = ProjectItem.find_books

Then in a _books.html.erb partial:

<% if @books%>   <% @books.each do |name| %>        <%= link_to ( #{},                 {:controller => "projects", :action => "show","} %>   <% end %> <% end %>

I am expecting that the selection creates an array where where @books[0][:name] is the name of a book.

Not so, evidently---or my Query is not correct?

@books is a collection of books, not names so

<% @books.each do |book| %>             <%= link_to ( #{},                {:controller => "projects", :action => "show"} %>        <% end %>

But this raises other questions.

First, why do you want to show a project when the user clicks on the book name, I'd expect you to show the book.

Second, why are you asking the ProjectItem class to find ALL of the books? You haven't said anything about the associations between project items and books, I'm guessing it's either

ProjectItem    has_many :books


Book    belongs_to :project_item

If a book can't be shared between project items,


ProjectItem    has_and_belongs_to_many :books

Book    has_and_belongs_to_many :project_items

if it can, and in the second case you might(should) use an explicit join model but for now lets assume that Books can't be shared between project items.

Given that I'd do something like

Book    belongs_to :project_item    named_scope :sorted_by_name, :order => " ASC"

ProjectItem    has_many :books

And then when you want the books associated with a particular project item, you can use



And by the way if you really want to get all the books sorted by name, you should ask the Book class, not the ProjectItem class, as in



I think you are misled because my syntax is perhaps wrong, but I did mention in my original question that 'book' is a field of ProjectItem. There is no 'Book' model.

So, in class ProjectItem I have a field 'book'. Each ProjectItem record will have a book field. Many books will have an identical name. I wish to make a list that is comprised of book names, of which each member of the list is a unique example of that book name. So there is no belongs_to or has_many relationship going on. I want this list of unique examples of book names to be sorted in ascending order.

When I am creating this instance:

   @books = ProjectItem.find_books

It would actually be a @project_items list, which to my understanding should only consist of data, since I believe :select => "DISTINCT(book)" only returns the data from that particular field (book) and not all the other data.

1)That is what I understand. If I am wrong on this, would like to know where.

2) I thought when I use 'name' in the following:

   @books.each do |name|               link_to ( #{} )

    ...that 'name' is a symbol only, and just used as a placeholder which        is just substituted for each member of the array. If that is correct,        then the above would just iterate through for each        and output a link to it. And @books is a var which actually is        an instance of @project_item. Part of reason for this posting        is to see if my assumptions are correct and I do have freedom        to name instance to something different than the standard approach.

       Perhaps using a standard naming approach would have increased the        clarity.


I found my error, it was syntax.

I used:

<% @books.each do |name| %> <% link_to ( #{} ) %> <% end %>

It should have been:

<% @books.each do |name| %> <% link_to ( %> <% end %>

Cheers, Jet