简体   繁体   中英

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?

你可以做:

resources :queued_workout, :controller => "workout"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM