1

I'm having some trouble with nested loops. Does anyone know a better way of doing this:

@product.tracks.each do |t|
    t.artists_tracks.each do |at|
        at.role = at.artist.role
        at.position = at.artist.position
        at.save
    end
end

I'm getting an undefined method role = error

Thanks in advance

1
  • Loop looks good to me. What attributes to you have on each models? Commented Aug 7, 2012 at 14:00

2 Answers 2

1
@product.tracks.each do |track|
    track.artists_tracks.each do |at|
        at.role = track.artist.role
        at.position = track.artist.position
        at.save
    end
end

But yeah.. sure thing you need to review your models attrs

Sign up to request clarification or add additional context in comments.

Comments

0

Some comments:

  • You should be able to write @product.artist_tracks provided you have a has_many :artist_tracks, :through => :artists.

  • at.role = at.artist.role. You are breaking the basic SQL rule of not having data duplicated, let the artist have the role.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.