Forum: Ruby on Rails output

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2006-01-23 23:32
(Received via mailing list)
I'm trying to build a page similar to a delicious page that is a listing
your bookmarks in this format:

   1. Querying serialized data in
   to search serialized fields
   to rails <> ... and 4 other
people<>... on
2006-01-17 ...
   2. ProgrammableWeb: Web 2.0 API
Reference<> Good
   listing of all the APIs
   to reference <>
programming<>... and
   85 other
on 2006-01-06 ...

There are several things going on there that make it
hard/impossible/inefficient to do it one query. I'm soliciting tips on
to do this most efficiently.

For me, the data is in 4 tables.
Bookmarks (id, bookmark)
Tags (id, tag)
Tags_Bookmarks (tag_id, bookmark_id, user_id)
Users (id, username)

I have a query that can get me most of the info I need, but not
in the format I need it in.

sql = "SELECT jt.tag_id, bid, t.tag, b.bookmark, jt.created_on
               FROM tags_bookmarks jt
               LEFT JOIN tags t ON jt.tag_id =
               LEFT JOIN bookmarks b ON jt.bookmark_id =
               WHERE jt.user_id = #{uid}"
               GROUP BY tag_id
              ORDER BY #{sort}"

That returns data in this format:

tag_id, bid, tag, bookmark, created_on
2, 23, Tag1, This is a bookmark, 12/25/06
3, 23, Tag2, This is a bookmark, 12/26/06
7, 25, Tag5, Different bookmark, 12/26/05

Problem 1: If I print out those results using a collection, "This is a
bookmark" would be shown twice - once tagged with Tag1 and again tagged
Tag2. I want to list to look like:

   1. This is a bookmark

   Tag1 <> Tag2 ... and 4 other
people<>... on
12/25/06 ...

...where Tag1 and Tag2 are shown within one bookmark. What can I do to
those listed under 1 bookmark?

Problem 2: What's the best way to get the "...and 4 other people" info?
one way I can think of is in my view, where I use a partial collection
print out each question, I call a helper function to count other users
bookmarking this url. For instance:


    <div id="your_bookmarks">
          <%= render :partial => "bookmark", :collection => @bookmarks

and then inside a partial _bookmark.rhtml:
      <% otherUserCount =

             <h4 class="bookmarkList"><%= link_to(bookmark.bookmark,
:controller => "content", :action => "bookmark") %></h4>
             <span class="smallLight">to <%= link_to(bookmark.tag)<snip>

If I list 20 questions, then that would mean 20 calls using the
call_to_helper... function. Is there a more efficient way?

Thanks very much for any suggestions.

Ad91bce9c9a8c68b6942607e4721f74e?d=identicon&s=25 Beate Paland (Guest)
on 2006-01-24 02:36
(Received via mailing list)
Hi Steve,

try to do it the railish way ;)

first, get away from writing SQL by hand. Have a look at
act_as_taggable. With this plugin you can easily tag items in a table.
Then connect links and user via belongs_to and so in your model.

I have your delicous-like look with my links, and get this with

    <h4><a href="<%= link.url %>"><%=h %></a></h4>

    <%= link.text %>

  <p>from <%= link.user.login %> in
	<% for tag in link.tag_names -%>
	  <%= link_to tag, :action => 'tag', :id => tag %>
	<% end -%>
(this is code in a partial)

In my controller I have no SQL:
@link_pages, @links = paginate :link, :per_page => 10, :order_by =>
'created_on DESC'

E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 Steve Odom (Guest)
on 2006-01-24 12:31
(Received via mailing list)
Hi Beate,

Thanks for your suggestions. I must admit sometimes I think it is easier
write the SQL by hand. You answered my question.

This topic is locked and can not be replied to.