ana
ana

Reputation: 43

Updating associations

I have lesson model that has many transcripts in different languages. I want to process each transcript and give it a title. I am unable to debug the code inside the each block, I am getting into the collection proxy class. What is the right way to do this?

My code:

class Lesson < ActiveRecord::Base
  has_many :lesson_transcripts

In the update method of controller:

def update
  @lesson = Lesson.find(params[:id])
  authorize! :update, @lesson
  @lesson.attributes = params[:lesson]

  @lesson.lesson_transcripts.each do |t|
    t.title = ...
  end
  @lesson.save
end

Upvotes: 0

Views: 39

Answers (1)

Gosha A
Gosha A

Reputation: 4570

Add the following line to your Lesson model:

accepts_nested_attributes_for :lesson_transcripts

Then, in your controller, you can simply have:

def update
  @lesson = Lesson.find params[:id]
  authorize! :update, @lesson

  @lesson.update_attributes params[:lessons]
end

Hope this helps.

Upvotes: 1

Related Questions