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

1 view
Skip to first unread message

BlueHandTalking

unread,
Nov 22, 2009, 4:20:46 PM11/22/09
to Ruby on Rails: Talk
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 ( #{name.book},
{: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?

Rick DeNatale

unread,
Nov 22, 2009, 4:55:07 PM11/22/09
to rubyonra...@googlegroups.com
@books is a collection of books, not names so

<% @books.each do |book| %>
<%= link_to ( #{book.name},
{: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

and

Book
belongs_to :project_item

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

or

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 => "books.name ASC"

ProjectItem
has_many :books

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

@project_item.books.sorted_by_name

HTH
--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Rick DeNatale

unread,
Nov 22, 2009, 4:56:49 PM11/22/09
to rubyonra...@googlegroups.com
On Sun, Nov 22, 2009 at 4:55 PM, Rick DeNatale <rick.d...@gmail.com> wrote:
> On Sun, Nov 22, 2009 at 4:20 PM, BlueHandTalking <j...@whidbey.com> wrote:

> Given that I'd do something like
>
> Book
>   belongs_to :project_item
>   named_scope :sorted_by_name, :order => "books.name ASC"
>
> ProjectItem
>   has_many :books
>
> And then when you want the books associated with a particular project
> item, you can use
>
>  @project_item.books.sorted_by_name
>

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

Book.all.sorted_by_name

BlueHandTalking

unread,
Nov 22, 2009, 8:58:32 PM11/22/09
to Ruby on Rails: Talk
Oh,

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 @project_items.book 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 ( #{name.book} )

...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
project_item.book
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.


Jet



On Nov 22, 1:56 pm, Rick DeNatale <rick.denat...@gmail.com> wrote:

BlueHandTalking

unread,
Nov 23, 2009, 1:22:49 AM11/23/09
to Ruby on Rails: Talk
I found my error, it was syntax.

I used:

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

It should have been:

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


Cheers,
Jet
Reply all
Reply to author
Forward
0 new messages