Rails model inheritance & routing
class User < ActiveRecord::Base
has_many :queued_workouts,
:conditions => "queue_position IS NOT NULL",
:order => "queue_position ASC"
has_many :workouts
end
class Workout < ActiveRecord开发者_运维百科::Base
end
class QueuedWorkout < Workout
# Have to give this its own class because acts_as_list always updates the
# queue_position when operating on user.workouts
acts_as_list :column => :queue_position, :scope => :user
end
I have routes for Workouts, but do not need them for QueuedWorkouts. Every once in a while I run into a case where I pass a QueuedWorkout instead of a Workout into url_for. In my specific case this is happening in WorkoutObserver.
Right now I'm doing
class WorkoutObserver < ActiveRecord::Observer
def after_update(workout)
workout = Workout.find(workout.id) if workout.is_a? QueuedWorkout
twitter_status = "some stuff " + short_url_for(workout) # Helper method for generating urls outside controllers & views
....
end
end
which, of course, is an awful solution. Is there a better way to tell the Rails router to generate Workout urls for QueuedWorkouts?
You can do:
resources :queued_workout, :controller => "workout"
精彩评论