开发者

Rails model inheritance & routing

开发者 https://www.devze.com 2023-04-09 02:49 出处:网络
class User < ActiveRecord::Base has_many :queued_workouts, :conditions => \"queue_position IS NOT NULL\",
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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号