1

Ok, so I have 4 models Users, Projects, Files, and ProjectsFiles

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

When creating a project I want my form to display a list the user's files with checkboxes. The user will be able to check files they want to add to the project which should build a ProjectsFile for each checked file and destroy any ProjectsFiles that aren't checked. Is this possible with accepts_nested_attributes_for and fields_for? I've been trying several different things but I can't get it right. What's a good way to go about this? Is there a better way?

jcon
  • 23
  • 1
  • 4
  • there have a similar question I answered before. [Rails forms for has_many through association with additional attributes](http://stackoverflow.com/questions/8352977/rails-forms-for-has-many-through-association-with-additional-attributes/8487188#8487188) – blade Jan 06 '12 at 02:14

1 Answers1

0

yes, you can use accepts_nested_attributes_for and fields_for, please have a view on this demo. this is source code of the demo.

hope this can help you.

blade
  • 434
  • 2
  • 10