Forum: Ruby on Rails Conditions on an Include (nuby on rails)

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.
Michael B. (Guest)
on 2007-02-08 08:23
I've got a database schema that is an artist with many tour_dates.  I'm
trying to grab the artist info and active tour dates at the same time.
Simple enough:

In the controller:
@artist = Artist.find(params[:id], :include => :tour_dates)


Now I want to include only the tour_dates that happen after the first of
the current month.  This is what I tried:

@artist = Artist.find(params[:id], :include => :tour_dates.active_dates)

and added

def self.active_dates
    find(:all,
         :conditions => "date > now()",
         :order => "date")
  end

to the tour_date model, just to see if i could get tour_dates after
right now to display.  It doesn't work quite the way I'd expect.  How
would you do this?  If you need to see more of my models or controllers
let me know.

I'm really just getting rolling on this so any help is appreciated.
I've got some books, I just didn't see anything like this in them.

I also am grabbing
@artists = Artist.active_artists (similar model to the above) for a menu
on the page, which I realize may be redundant, but I can't figure out
how to grab all the artist names and ids, but also everything for one
specific artist.

Thanks!
Charly (Guest)
on 2007-02-08 14:24
(Received via mailing list)
Hi,
you would probably want to do this in yout Artist model
def active_dates
  tour_dates.find(:all, :conditions => "date >
now()", :order=>'dates')
end

or with an eager load(@artist =
Artist.find :all, :include=>:tour_dates) (no attributes here
on :tour_dates)

def active_dates
  tour_dates.select {|date| d >now()}
end

Charly


On 8 fév, 07:23, Michael B. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.