Is there a way to avoid automatically saving an object when assigning collection attributes (collection_singular_ids = ids method)?
for example, I have the following test and package model, the package has many tests. The user can create a batch package with a number of tests.
# test.rb class Test < ActiveRecord::Base end
There is no validation issue when the package object is new .
1.9.2 :001> package = Package.new(:name => "sample", :cost => 3.3, :test_ids => [1,2,3,4]) =>
But I could not use the at_most_3_tests method with the saved package object.
Registration of a record in a table is created immediately when assigning test identifiers
1.9.2: 006> package => #<Package id: 1, name: "sample", cost: 3.3> 1.9.2: 007> package.test_ids => [1,2] 1.9.2: 007> package.test_ids = [1,2,3,4,5] => [1,2,3,4,5] 1.9.2: 008> package.test_ids => [1,2,3,4,5]
The client requirement is a drop-down interface for selecting several tests in the form of a package and also use the select2 jquery plugin for the drop-down list. Rhmtl code looks like
<%= form_for @package do |f| %> <%= f.text_field :name %> <div> <label>Select Tests</label> </div> <div> <%= f.select "test_ids", options_for_select(@tests, f.object.test_ids), {}, { "data-validate" => true, :multiple => true} %> </div>
Please help me solve this problem.