ruby on rails - Validating length of habtm association without saving -


I have user models with regards to an HABTM group. I do not want a user to be capable of more than 5 groups, In this way, the HAABT wants to validate the length of the relationship.

On the Edit User page, I have a list of checkboxes where users can select the groups they want (I am using formattic for the form).

I am calling in my User Controller:

  @ user.update_attributes (Param [: User])  

I have the following in my user model:

  DIF valid if self.groups.length & gt; ; 5 self.errors.add (: groups, "can not be more than 5 groups") Ending  

This is failing the form due to verification, but the update_attributes call first To display changes in the related groups, the database clicks on the user's save button every time, their group associations are saved even if the record is invalid.

What is the best way to resolve this?

I think the legalization needs to be done on the group model rather than the user model, will it work? Ideally, I want to update, verify, and then save the records to related groups without saving records.

  1. Override assumptions
  2. Savings In the order of action, problems are arising.

You are overwriting a valid method which is a bad thing, because in practice it is built in practice to reject records with verification errors to be saved in the database. To add custom assumptions, you want to:

  Validate: Max_Graphy_LampR max_group_length if self.groups.length & gt; 5 self Errors.Add (The group, "can not be more than 5 groups") End of End  

However, for the nature of HABTM relations you have to do this as an after_save callback . Just because things have been completed user.groups is based on the built-in attendant table and is not updated until it is included.

If you are trying to validate as part of the callback (Before_save, after_creation, etc.), then a roll will not trigger back to add an error to the object. Callback only triggers a rollback if they return false. After saving this implementation, it will suggest the question.

  after_save: validate_maximum_group_length def validate_maximum_group_length if self.groups.length & gt; 5 self.errors.add (: groups, "can not be more than 5 groups") Back to the wrong end and return  

Another solution is to use a clear joining model. And there is one: The table of models to connect through the relationship has been updated in the updated statement, where is: _Maya: By updating relationship and after relationships with HABTM relationships.

  class user & lt; ActiveRecord :: Base has has_many: user_groups has_many: groups ,: through = = & gt; User_groups, allow_destroy valid: max_group_length errors.add (: group, "can not be more than 5 groups") if self.user_groups.length & gt; 5 End and Class UserGroup & lt; ActiveRecord :: Base is_to: User belongs_to: Group end group group & lt; ActiveRecord :: Base has_and_belongs_to_many: User End  

HABTM basically uses an included table, so there is no need to change it on the group side.

However, you need to modify your form to update the form to supply the group code in parameter hashes. params [: user] [: user_group_attributes] [0] [: group_id ] [3]


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -