1
class Chair < ActiveRecord::Base 
    has_many :buildings, :as => :faculty 
end 

class Department < ActiveRecord::Base 
    has_many :buildings, :as => :faculty 
end 

class Building < ActiveRecord::Base 
    belongs_to :faculty, :polymorphic => true 
end 

在routes文件,我有:如何把這種多態模型的「所有者」在Rails3中

resources :departments do 
    resources :buildings do 
end 

resources :chairs do 
    resources :buildings do 
end 

如何建立良好的方式在一個控制器部和主席的新大樓。我知道,我可以創建另一個有用的功能,它會檢查誰創造大廈:

class BuildingsController < ApplicationController 
    def new 
    @parent = faculty 
    @building = @parent.buildings.build 
    end 
end 
在application_controller

def faculty 
    if params[:department_id] 
    Department.find(params[:department_id]) 
    elsif params[:chair_id] 
    Chair.find(params[:chair_id]) 
    end 
end 

,但我問,是否有任何其他鐵路公司的方式做它?也許Rails3中有一些方法可以用來獲取信息:誰想要創建構建,而不需要實現其他功能。

+0

爲什麼你感動的教師到應用控制器?把它移到「建築物」。試着從建築物/椅子和建築物/部門那裏思考,如果不是,那麼這是你如何做的最好方式。 – 2011-01-24 04:07:18

回答

相關問題