0

I have a view where I am trying to display all the objects in an array. The objects are tickets.

<tbody>
    <% @tickets.each do |ticket| %>
        <tr>
            <td><%= ticket.ticket_number %></td>
            <td><%= ticket.title %></td>
            <td><%= ticket.product_name %></td>
            <td><%= truncate(ticket.description) %></td>
            <td><%= ticket.status %></td>
            <td><%= link_to "Show", users_ticket_path(ticket) %>
        </tr>
    <% end %>
</tbody>

My controller is finding all Users that have the same 'reseller_id' as the current User. So say I have 3 Users all under the same Reseller. The 3 Users all have the same 'reseller_id' Here is my controller that doesn't work.

def index
  if admin_user?
    @test = "HELLO"
    all_users = User.find_all_by_reseller_id(current_user.reseller_id)
    all_users.each do |u|
      @tickets = u.tickets
    end
  #@tickets = @tickets.paginate(page: params[:page])
  else
    @test = "WORLD"
    @tickets = current_user.tickets.paginate(page: params[:page])
end
end

This saves a blank array. How do I make an array with all the ticket objects?

Thanks

1
  • Would anyone know why this works in development mode and not in production mode? Commented Sep 19, 2013 at 23:01

2 Answers 2

2

Currently @tickets is equal to tickets of last user found. Use something like this

def index
  @tickets = []
  if admin_user?
    @test = "HELLO"
    all_users = User.find_all_by_reseller_id(current_user.reseller_id)
    all_users.each do |u|
      @tickets += u.tickets
    end
  #@tickets = @tickets.paginate(page: params[:page])
  else
    @test = "WORLD"
    @tickets = current_user.tickets.paginate(page: params[:page])
end
end
Sign up to request clarification or add additional context in comments.

9 Comments

I did not know I could use += awesome
Do you know why this works in development mode but now in production mode?
what error are you getting in production? If you get empty @tickets in production while non-empty @tickets in development then it could be due to lack of data in production db.
I do not get an error. The array is displaying only the current_user and not the other User's tickets in production. All three users have tickets.
seems like the production app has not been restarted to use the new code.
|
0

Or you can do it in one line

def index
  if admin_user?
    @test = "HELLO"
    @tickets = Ticket.where("user_id IN (?)", User.find_all_by_reseller_id(current_user.reseller_id).map(&:id))
    @tickets = @tickets.paginate(page: params[:page])
  else
    @test = "WORLD"
    @tickets = current_user.tickets.paginate(page: params[:page])
  end
end

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.