I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it not create new records if a record already exists?
I have found this article, accepts_nested_attributes_for with find_or_create? but it doesn't seem to work in my case.
I have a many-to-many relationship between packages and licenses. There should only be one instance of a license in the table, for instance: I shouldn't have two licenses named, "Awesome"
The desired functionality is that when I create a package, passing in a license name as an attribute, create a new license if the name doesn't already exist or create an association between the existing license (with the provided name) and the package.
Here are what my models look like:
class Package < ActiveRecord::Base
has_and_belongs_to_many :licenses, :autosave => true
accepts_nested_attributes_for :licenses
end
class License < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :packages
validates :name, :presence => true
end