Update entry in a has_many relation

Hello all,
I want to upate a entry in a one to many relation, but is doesn’t work.
I can add or delete a specific entry, but I can’t update it.

Can anyone help me?

Here is the code:

class Person < ActiveRecord::Base
has_many :materials
end

class Material < ActiveRecord::Base
belongs_to: person
end

class PersonController < ApplicationController

def update
person = Person.find(@params[“id”])
person.name = @params[“name”]
first_material = person.materials.find(@params[“material_id”])
first_material.size = @params[“material_size”]
person.update
end

def add
person = Person.new
person.name = @params[“name”]
material = person.materials.find(@params[“material_id”])
person.materials.push(material)
person.update
end

def delete_material
person = Person.find(@params[“id”])
person.name = @params[“name”]
material = person.materials.find(@params[“material_id”])
person.materials.delete(material)
person.update
end

end

Thanks,
H

Sorry, controller code is wrong.
Here is the correct one:

class PersonController < ApplicationController

def update
person = Person.find(@params[“id”])
person.name = @params[“name”]
first_material = person.materials.find(@params[“material_id”])
first_material.size = @params[“material_size”]
person.save
end

def add
person = Person.new
person.name = @params[“name”]
material = person.materials.find(@params[“material_id”])
person.materials.push(material)
person.save
end

def delete_material
person = Person.find(@params[“id”])
person.name = @params[“name”]
material = person.materials.find(@params[“material_id”])
person.materials.delete(material)
person.save
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs