2012-01-06 64 views
1

好了,我有4級車型的用戶,項目文件和ProjectsFiles通過:, accepts_nested_attributes_for用的has_many工作,並fields_for

class User < ActiveRecord::Base 
    has_many :files 
end 

class Project < ActiveRecord::Base 
    has_many :projects_files 
    has_many :files, through: :projects_files 
end 

class File < ActiveRecord::Base 
    has_many :projects_files 
    has_many :projects, through: :projects_files 
end 

class ProjectsFiles < ActiveRecord::Base 
    belongs_to :project 
    belongs_to :file 
end 

當創建一個項目,我想我的形式顯示列表中的用戶的文件與複選框。用戶將能夠檢查他們想要添加到項目的文件,該文件應該爲每個選中的文件構建一個ProjectsFile並銷燬任何未被選中的ProjectsFiles。這是可能的與accep_nested_attributes_for和fields_for?我一直在嘗試幾種不同的東西,但我無法做到。有什麼好辦法可以解決這個問題?有沒有更好的辦法?

+0

有一個類似的問題,我回答以前。 [通過與附加屬性關聯的Rails表單has_many](http://stackoverflow.com/questions/8352977/rails-forms-for-has-many-through-association-with-additional-attributes/8487188#8487188) – blade 2012-01-06 02:14:17

回答

0

是的,您可以使用accep_nested_attributes_for和fields_for,請對demo有看法。這是演示的source code

希望這可以幫助你。