— quote —
Is there any way I can integrate these two things without
breaking AR and maintaining DRY? The logic is replicated once in
viewable_listings or equivalent and once in find_viewable.
— quote —
so just to make sure i understand this correctly, you want a single
to be able to return either a single viewable listing or multiple
listings depending on the argiments? sounds just like what
AR::Base.finddoes for you already, for the most part.
if you think about it, you’d basically be rewriting AR::Base.find with
hardcoded conditions (whatever makes a listing ‘viewable’). all this
do is save you from having to specify your conditions in the find call.
if you step back and think about it for a minute, this is the way you
normally do it.
@viewable_listing = Listing.find(params[:id], :conditions => “…”)
# handle record not found exception here
@viewable_listings = Category.listings.find(:all, :conditions =>
with your idea, you want to be able to do:
@viewable_listing = Listing.find_viewable(params[:id])
# handle record not found exception
@viewable_listings = Category.listings.find_viewable(:all)
notice the only thing i’ve changed is the method name and removed the
:conditions, as the conditions would be handled within your custom
class Listing < ActiveRecord::Base
conditions = “…”
# merge in your custom ‘viewable’ conditions into the arguments and
merging your custom conditions is beyond my abilities at the moment, but
think this is what you are after. you could probably take a look at the
source to AR::Base.find to figure it out.
hope this helps.