简体   繁体   English

Rails模型继承和路由

[英]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. 我有用于锻炼的路线,但对于QueuedWorkout则不需要它们。 Every once in a while I run into a case where I pass a QueuedWorkout instead of a Workout into url_for. 偶尔我遇到一种情况,我将QueuedWorkout而不是Workout传递给url_for。 In my specific case this is happening in WorkoutObserver. 在我的特定情况下,这是在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? 有没有更好的方法告诉Rails路由器为QueuedWorkouts生成锻炼URL?

你可以做:

resources :queued_workout, :controller => "workout"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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