Rails 3将对象附加到数组

时间:2021-12-25 21:15:55

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.

我的控制器正在查找与当前用户具有相同“reseller_id”的所有用户。所以说我有3个用户都在同一个经销商下。 3个用户都有相同的'reseller_id'这是我的控制器不起作用。

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

2 个解决方案

#1


2  

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

目前@tickets等于找到的最后一个用户的门票。使用这样的东西

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

#2


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

#1


2  

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

目前@tickets等于找到的最后一个用户的门票。使用这样的东西

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

#2


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