0
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
我有鍛鍊的路線,但不需要它們用於QueuedWorkouts。每過一段時間,我都遇到了將QueuedWorkout而不是Workout傳遞到url_for的情況。在我的具體情況下,這發生在WorkoutObserver中。Rails模型繼承和路由
現在我做
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
,當然這是一個可怕的解決方案。有沒有更好的方法來告訴Rails路由器爲QueuedWorkouts生成鍛鍊URL?
因爲我已經切換到這一點,但寧願有路由器自動複製路線。或跳過路由並在url_for中執行。 –