views:

266

answers:

1

I have a rails site with two models: events and dates

events has many dates

events are tagged using acts_as_taggable_on

I'm trying to find all dates (chronological order) for events tagged with a specific tag. I can figure out how to get all the tagged events, but I can't figure out how to get all of the dates.

The end result I'm looking for is to create a calendar of similar events where events appear on each of their dates.

Hopefully that makes sense, thanks for any and all input!

A: 

As you know Events.tagged_with("my_tag") returns a list of events matching your tags.

You can then use the map or collect operator on this list to get a list of dates for each event.

`map{|e| e.dates}`

This returns a list of date arrays, where each array the list of dates associated with the event in that index of the list returned by Events.tagged_with("my_tag")

To order by date you need to flatten and sort.

flatten.sort{|a,b| a.created_at <=> b.created_at}

Making the entire method call:

Events.tagged_with("my_tag").map{|e| e.dates}.flatten.sort{|a,b| a.created_at <=> b.created_at}

You can simplify this with a named scope on dates. It will be a faster query, but I can't write one for you without knowing more about the structure of your database.

It would look something like this, assuming dates belong to an event:

class Date < ActiveRecord::Base
  ...
  named_scope :for_tag, lambda do |tag|
    {:joins => [:tags, :taggings], 
     :conditions => ["tags.name = ? AND tags.id = taggings.tag_id \
       AND taggings.taggable_id = dates.event_id AND \
       taggings.taggable_type = event", tag],
     :order => 'dates.created_at'}
  end  
end

You could then do Date.for_tag("my_tag") and retrieve the same list, in a much more efficient SQL query.

EmFi
Thanks, the first solution worked perfectly. The second one I'm having trouble with (probably because you don't know the exact db structure like you said). I've adjusted it to the best of my abilities, but I'm getting an error saying the association for "tags" doesn't exist. After looking into it a bit, it looks like it's being caused by the :joins portion of the code because there isn't a tags assosciation for dates. Any ideas?
Ryan
I haven't used acts_as_taggable_on so I assumed it implicitly defined a has_many relationship with tags, it must have another name. Have a look at the acts\_as\_taggable\_on source to figure out which tables to join. As a last resort I could look at it, but you would need to post the bits of your models that define relationships and tags. As in only the lines with has\_many/belongs\_to/acts\_as\_taggable/acts\_as\_taggable\_on
EmFi
Now that I understand it's a relationship issue, I should be able to figure it out later today. Sorry for the delay and thanks for all your help. I'll post here when I'm done for anyone else who may have the same issue.
Ryan
Don't worry about a Delay. It's not like any one really needs the Reputation.
EmFi