簡體   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

我有用於鍛煉的路線,但對於QueuedWorkout則不需要它們。 偶爾我遇到一種情況,我將QueuedWorkout而不是Workout傳遞給url_for。 在我的特定情況下,這是在WorkoutObserver中發生的。

現在我在做

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?

你可以做:

resources :queued_workout, :controller => "workout"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM