Ruby on Rails -- Saving and updating an attribute in a join table with has many => through -


to simplify things, have 3 tables :

person

has_many :abilities, through => :stats

ability

has_many :people, through => :stats

stats

belongs_to :people

belongs_to :abilities

stats has attribute called 'rating'.

what i'd make edit person form lists abilities in database, , lets me assign each 1 rating.

for life of me, can't figure out how this. managed work when creating new user this:

(from people controller)

def new  @character = character.new     @abilities = ability.all     @abilities.each |ability|         @person.stats.build(:ability_id => ability.id )     end end  people form:  <% @ability in @abilities %>     <%= fields_for "person[stats_attributes]" |t| %>  <div class="field">     <%= t.label @ability.name %>     <%= t.hidden_field :ability_id, :value => @ability.id, :index => nil %>     <%= t.text_field :rating, :index => nil %>   </div>   <% end %>   <% end %> 

this gives me list of abilities ratings boxes next them, , lets me save them if i'm making new user.

the problem if load edit form (using same form partial), doesn't bring ratings, , if save, exact same ratings, creates duplicate entries in stats table, instead of updating it.

i realize i'm terrible programmer , i'm doing wrong way, how edit form recall current ratings assigned each ability user, , secondly how update rating instead of duplicating if combination of person , ability exists?

shouldn't

character has_many :stats has_many :abilities, through => :stats  ability has_many :stats has_many :characters, through => :stats  stat belongs_to :character belongs_to :ability 

?

also, person or character? refer variously both. (i'm going go character in answer)

i think you've fallen foul of "i'll try make simplified version of schema in order attempt illustrate problem instead make things more complex , muddle issue screwing doesn't make sense" syndrome. anyway, there's couple of issues can see:

1) first thing you're adding possible abilities character they're created. silly - should start out no abilities default , create join table records (stats) ones do have (by ticking checkboxes in form).

2) simple way manipulate join records leverage "ability_ids=" method has_many :abilities macro gives - referred "collection_ids=" in api http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=m001885&name=has_many

in other words, if

@character.ability_ids = [1,12,30]

then make joins between character , abilities 1, 12 , 30 , delete other joins between character , abilities not in above list. combine fact form field names ending in [] put values array, , can following:

#controller def new  @character = character.new  @abilities = ability.all end  #form <% @abilities.each |ability| %>   <div class="field">     <%= t.label @ability.name %>     <%= check_box_tag "character[ability_ids][]" %>   </div> <% end %>  #subsequent controller (create action) @character = character.new(params[:character]) #totally standard code 

notice there's no mention of stats here @ all. specify associations want between characters , abilities , let rails handle joins.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -