activerecord - Updating extra attributes in a has_many, :through relationship using Rails -


I have been able to establish many-to-many relationships between the following models

  • Skills Player
  • Player Skills, is a feature that is not normally in skill: A level. / P>

    Models look like this (edited for condensation):

      class player scale & lt; ActiveRecord :: Base is_to: Character is_to: Skill end class skills & lt; ActiveRecord :: Base is_maya: player_skills_images: characters ,: through = = gt; : Player_skills attr_accessible: Name, Description End Category Characters & lt; ActiveRecord :: Base is_mto: The user has_many: player_skills_im I: skill ,: through => : Player_skills end  

    Then there is nothing fancy in the model ... The controller is also very basic at this point ... it is quite a stock update action.

    The form I'm modifying is the letter # edited. There is now a series of check boxes that allow characters to add / remove skills. It is very good, but the whole thing to use it was to track a "level" through the "medium".

    Even so far away from me:

      - let's form_for @ charter. F | = F.error_messages% p = f.label: name% br = f.text_field: name% p = f.label: race% br = f.text_field: race% p = f.label: char_class% br = f.text_field : Char_class% p - @ skills.each do | Skills | = Check_box_tag "character [skill_ids] []", skill.id, @ character.skills.include? After the name (skill) = h skill.name% br% p = f.submit  

    it names the "skill", I need to print it to text_field which will be updated to player_skill Does.

    The problem, obviously that player_skill may or may not exist! (Depending on the box that was already checked when you filled the form!)

    I have read what I_many: through is great because it gives you Allows to treat the relationship as a unit ... but I have been completely damaged about how to handle the unit in this form.

    As usual, thanks for any and all help, you can give me first!

I have fixed this problem so far that I am doing ...

It was actually relatively straight forward, I learned about nested characteristics!

Here's the new letter model!

  class character & lt; ActiveRecord :: Base is_mto: The user has_many: player_skills_im I: skill ,: through => : Player_skills accepts_name_data_to: player_skill def skills_pre_update (params) skills = skill Find (: All,: Order = & gt; 'ID') Skill = Skill Mapping | Skills | Skill.id end self.skill_ids = [] self.skill_ids = Skill self.skill_ids.each_with_index do | Skill_id, index | [Interval score] [interval] [interaction] [interaction] [interaction] p> and the updated action for character controller was lightly changed:  
  @ character.skills_pre_update ( Params [: Characters] .delete (: player_skills_attributes) Ultimate [: Character] .delete (: Skill_ids)  

Reason, those two parts are already controlled by pre_update operation , So it is not necessary to handle them again by update_attributes, which is said later.

This view was relatively straight forward. Many checkboxes are still the same, though I've added new text boxes!

  - @ skills.each_with_index do | Skills, index = Check_box_tag "character [skill_ids] []", skill.id, @ character.skills.include? (Skill) = h Skill name -ps = skill.player_skills.find_by_character_id (@character) || Skill.player_skills.build -fields_for "Character [player_skills_attributes] []", ps do | Psf | = Psf.text_field (: level :: index = & gt; zero) = psf.hidden_field (: id, index = & gt; zero)  

In short, Skill_ids ( skill_ids = [] ) out of the model because otherwise it sets improperly.

In short, I add all the skills.
Update the levels, using the text box.
Then reset the user's actual testing skills (which will remove any unused skills.)

I do not think this is the biggest solution - but rather laughs for me. . So if someone wants to tilt with a better, possibly faster / more elegant solution, feel free!

Otherwise, I hope it will help someone else ... because modifying the extra features on the joining table (joining tables without giving their controller / view) was a real pain!


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -