2012-01-06 65 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

希望这可以帮助你。