1

I'm trying to do a for each loop to update my database. I was using this piece of code to make the update:

def update
  respond_to do |format|
    @t_id = params[:t_id]
    @t_order = params[:order]
    @t_relation = TRelation.where('t_id' => @t_id)
    @i = 0;
    @t_order.each do |p|
      @t_relation = TRelation.where('t_id = ? and
        video_id = ?', @t_id, p[1][@i])
      @i = @i + 1
      @t_relation[0].t_order = @i
      @t_relation[0].save
    end
    format.json { render :nothing => true, :status => 200, :content_type => 'text/html' }
  end
end

It does not loop through; it goes through it one time and stops. I don't understand what's happening.

This is the content of params[:order]

params[:order] = {ActionController::Parameters} ActionController::Parameters (1 element)
 '0' = Array (3 elements)
  [0] = "7"
  [1] = "5"
  [2] = "3"

And if I make a @timeline_order.inspect I get this:

{"0"=>["7", "5", "3"]}

How can I loop through it? I have no idea

1
  • @t_order["0"].each ? Commented Dec 15, 2015 at 23:17

1 Answer 1

1

It iterates once because params[:order] only has one element. What you want to do is iterate on @t_order["0"], which has 3 elements.

Also, you should avoid all that logic within the respond_to. You can (should) define the variables outside of it.

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

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.