has many through - Rails 3 can't perform validation for persited object when use collection_singular_ids=ids method -


Is there a way to avoid saving the object while specifying the collection attributes (archive_community_id = id method)?

For example, I have the following tests and package models, there are many tests in the package, the user can create package bundles with the number of tests.

  # test.rb class test & lt; ActiveRecord :: Base End # package.rb class package & lt; ActiveRecord :: Base has has_many: package_tests has_many: tests ,: through = & gt; : Package_tests belongs_to: Validate Category: at_most_3_tests Personal # The calculation of the exams will be different depending on the category. Def at_most_3_tests errors.add (: base, 'this package should have a maximum of three trials') if test_ids.count & gt; 3rd End # package_test.rb class PackageTest & lt; 

Code> 1. 9.2: 001 & gt; Package = Package.New (: name = & gt; "Sample" ,: Cost => 3.3,: test_ids = & gt; [1,2,3,4]) = & gt; # & Lt; Package ID: Zero, Name: "Sample", Cost: 3.3 & gt; 1. 9.2: 002 & gt; Package.test_ids = & gt; [1, 2, 3, 4] 1.9.2: 003 & gt; Package.save = & gt; False 1.9.2: 004 & gt; Package.save! ActiveRecord :: RecordInvalid: Validation Failed: This package can have up to three trials 1.9.2: 005 & gt; Package.test_ids = [1,2] = & gt; [1, 2] 1.9.2: 005 & gt; Package.save! = & Gt; True

but I could not hit the at_most_3_test method with the constant package object.

The test is made immediately upon joining

  1.9.2: 006 & gt; Package = & gt; # & Lt; Package ID: 1, Name: "Sample", Cost: 3.3 & gt; 1.9.2: 007 & gt; Package.test_ids = & gt; [1,2] 1.9.2: 007 & gt; Package.test_ids = [1,2,3,4,5] = & gt; [1,2,3,4,5] 1.9.2: 008 & gt; Package.test_ids = & gt; [1,2,3,4,5]  

There is a drop-down interface for selecting multiple tests in the form of a client package and also for the drop-down jquery plugin Has been used. Rhmtl code looks like

  <% = form_for @package do | F | & Gt%; & Lt;% = f.text_field: name% & gt; & Lt; Div & gt; & Lt; Label & gt; Choose Test & lt; / Label & gt; & Lt; / Div & gt; & Lt; Div & gt; & Lt;% = f.select "test_ids", options_for_select (@tests, f.object.test_ids), {}, {"data-valid" = & gt; True ,: Multiple = & gt; True}%> & Lt; / Div & gt;  

Please help me fix this problem.

for limitations of associations

Instead of your method, you can use the following assumptions:

  has_many: tests, length = & gt; {: Max = & gt; 3}  

To use multiple selection

I have this problem before, and I solved it using the following code Is:

  & lt;% = f.select (: test_ids, options_from_collection_for_select (@tests ,: id ,: name, @ promotion.test_ids), {}, {multiple: true, Data-Valid "= & gt; True}} =>  

I think it can help you read categories of post examples.

< Strong> Validation

I used, as the following:

  is valid_assigned: test   

To get past properties for continuous objects

You can use it for active records as:

  1.9.2: 006 & gt; Package = & gt; # & lt; Package ID: 1, Name: "Sample", Cost: 3.3 & gt; 1.9.2: 007 & gt; Package.test_ids = Gt; [1,2] 1.9.2: 007 & gt; Package.test_ids = [1,2,3,4,5] = & gt; [1,2,3,4,5] 1.9.2: 007 & Gt; Package.reload = & gt; # & lt; Package ID: 1, Name: "Sample", Cost: 3.3 & gt; 1. 9.2: 008 & gt; Package.test_ids = & gt; [1,2]  

Or you can check the validity of the package object, if it is incorrect, it can be reloaded:

  Unless package.valid happens? Package.reload end  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -