Domanda

I'm kinda new to coding on rails. It would be great if you could help me out with what I think might be noob question.Here's my code:

def create
@project = Project.new(params[:project])
if @project.save
    redirect_to new_project_path
end

student=@project.student_str.split(";")
@users = User.where(:code => student)

@users.each do |c|
puts c.email
end

@users.each do |c|
puts "I'm here"
c.projects = "#{c.projects};#{@project.id}"

end



end

So, in the create method, Each time a new project is created a string called student_str is stored where the ID number of each student is seperated by a ";". I split that string to an array using the split function to get an array of student ID's. I have the puts c.email and puts "I'm here" to make sure the loops are working fine. I get the proper outputs on terminal.

The problem here is the

c.projects = "#{c.projects};#{@project.id}"

That simply does not seem to be working. My model is not updated when this line is executed. I get no errors though. Can you tell me what I might have to do to fix this?

thanks!

È stato utile?

Soluzione

You have to call c.save after you updated the projects attribute. Otherwise the object is updated but not the database so the next time you load it the changes are gone.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top