Forum: Ruby on Rails del.icio.us-like output

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
of
your bookmarks in this format:


   1. Querying serialized data in
ActiveRecord<http://www.karmiccoding.com/articles/2005/11/29/qu...
Hack
   to search serialized fields
   to rails <http://del.icio.us/djsodom/rails> ... and 4 other
people<http://del.icio.us/url/acd90e7ad2ec718ead225bc48d7c68dc>... on
2006-01-17 ...
   edit<http://del.icio.us/djsodom?url=http%3A%2F%2Fwww.ka...
   delete<http://del.icio.us/djsodom?delete=acd90e7ad2ec718e...
   2. ProgrammableWeb: Web 2.0 API
Reference<http://programmableweb.com/apis> Good
   listing of all the APIs
   to reference <http://del.icio.us/djsodom/reference>
programming<http://del.icio.us/djsodom/programming>... and
   85 other
people<http://del.icio.us/url/76b171f78b8e4eb0851ae24a1994f105>...
on 2006-01-06 ...
   edit<http://del.icio.us/djsodom?url=http%3A%2F%2Fprogra...
   delete<http://del.icio.us/djsodom?delete=76b171f78b8e4eb0...

There are several things going on there that make it
hard/impossible/inefficient to do it one query. I'm soliciting tips on
how
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
necessarily
in the format I need it in.

sql = "SELECT jt.tag_id, b.id bid, t.tag, b.bookmark, jt.created_on
               FROM tags_bookmarks jt
               LEFT JOIN tags t ON jt.tag_id = t.id
               LEFT JOIN bookmarks b ON jt.bookmark_id = b.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
with
Tag2. I want to list to look like:


   1. This is a bookmark

   <http://www.karmiccoding.com/articles/2005/11/29/qu...
   Tag1 <http://del.icio.us/djsodom/rails> Tag2 ... and 4 other
people<http://del.icio.us/url/acd90e7ad2ec718ead225bc48d7c68dc>... on
12/25/06 ...
   edit<http://del.icio.us/djsodom?url=http%3A%2F%2Fwww.ka...
   delete<http://del.icio.us/djsodom?delete=acd90e7ad2ec718e...

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

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

userBookmarks.rhtml

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

and then inside a partial _bookmark.rhtml:
      <% otherUserCount =
call_to_helper_that_counts_other_users_also_bookmarking_this_url(
bookmark.bookmark_id)

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

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.

Steve
http://www.smarkets.net
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 link.name %></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'

HTH,
Beate
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
to
write the SQL by hand. You answered my question.

Steve
http://www.smarkets.net
This topic is locked and can not be replied to.