Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 表rails中ActiveRecord::Associations::CollectionProxy的排序顺序_Ruby On Rails_Ruby_Sorting_Activerecord - Fatal编程技术网

Ruby on rails 表rails中ActiveRecord::Associations::CollectionProxy的排序顺序

Ruby on rails 表rails中ActiveRecord::Associations::CollectionProxy的排序顺序,ruby-on-rails,ruby,sorting,activerecord,Ruby On Rails,Ruby,Sorting,Activerecord,我想在表中设置活动记录收集代理的排序顺序。 它应该按可用房间的数量从高到低排序。 诀窍在于@rooms.reserved是一个布尔值,为了计算空闲/保留房间的数量,我必须使用helper方法来避免记录收集代理错误。我得到了正确的结果,但我需要根据可用房间的数量对表进行排序 我有两种型号:客房和酒店 class Room < ApplicationRecord belongs_to :hotel, optional: true # avoiding rails 5.2 belongs_t

我想在表中设置活动记录收集代理的排序顺序。 它应该按可用房间的数量从高到低排序。 诀窍在于@rooms.reserved是一个布尔值,为了计算空闲/保留房间的数量,我必须使用helper方法来避免记录收集代理错误。我得到了正确的结果,但我需要根据可用房间的数量对表进行排序

我有两种型号:客房和酒店

class Room < ApplicationRecord
  belongs_to :hotel, optional: true # avoiding rails 5.2 belongs_to error 
  accepts_nested_attributes_for :hotel
end
房间的活动记录表:

class CreateRooms < ActiveRecord::Migration[5.1]
  def change
    create_table :rooms do |t|
      t.boolean :reserved, :default => false
      t.belongs_to :hotel, index: true

      t.timestamps
    end
  end
end

我会在Room模型上添加一个类方法,以返回给定集合的免费房间和保留房间的数量:

class Room < ApplicationRecord
  belongs_to :hotel, optional: true
  accepts_nested_attributes_for :hotel

  def self.reserved_count
    where(reserved: true).count
  end

  def self.free_count
    where(reserved: false).count
  end
end
您的视图最终将如下所示:

<table>
  <tr>
    <th>Name</th>
    <th>Rooms count</th>
    <th>Rooms status: in reserved || free</th>    
  </tr>

  <% @hotels.each do |hotel| %>
    <tr>
      <td><%= hotel.name %></td>
      <td><%= hotel.rooms_count %></td>
      <td><%= "#{hotel.reserved_rooms} || #{hotel.free_rooms}" %></td>      
      <td ><%= link_to 'Show', hotel_path(hotel) %></td>
      <td><%= link_to 'Destroy', hotel, method: :delete, data: { confirm: 'Are you sure?' } %>
    </tr>
  <% end %>
</table>
您最终可以将其实现为Hotel.includes:rooms.sort\u by&:free\u rooms.reverse

这样,您就不需要任何连接或帮助程序


关于您的评论,free_rooms是作为实例方法实现的,例如Hotel.first.free_rooms,因此它将不适用于ActiveRecord_关系,例如Hotel.all.free_rooms

实现联接和where子句,然后按和计数分组。所有这些都可以在活动记录中完成。还有,你在使用哪个数据库?@user1735921我以前从未使用过这种技术。我使用默认的SQLite@hotels=Hotel。选择“count*作为免费房间”。加入:rooms.group“rooms.id”。订购“free\u rooms DESC”,然后按Hotel.free访问房间_rooms@user1735921我相信我做错了什么。你能给我一个更宽泛的回答吗,谢谢。最好的方法不仅仅是解决问题,还要理解解决问题。对不起,我知道的不多,帮不了你更多。我正在尽我最大的努力。你的代码只是我的。当我试图通过控制器对它们进行排序时,它仍然是未定义的方法“free_rooms”。这是正确的行为,因为我不能直接进入免费房间。我只能访问ActiveRecord\u AssociationRelation。不幸的是,我无法正确排序ActiveRecord\u AssociationRelation。这就是问题所在。@KirillZhuravlov此更新是否回答了您原来的问题?
class CreateRooms < ActiveRecord::Migration[5.1]
  def change
    create_table :rooms do |t|
      t.boolean :reserved, :default => false
      t.belongs_to :hotel, index: true

      t.timestamps
    end
  end
end
class Room < ApplicationRecord
  belongs_to :hotel, optional: true
  accepts_nested_attributes_for :hotel

  def self.reserved_count
    where(reserved: true).count
  end

  def self.free_count
    where(reserved: false).count
  end
end
class Hotel < ApplicationRecord
  has_many :rooms, dependent: :destroy
  accepts_nested_attributes_for :rooms

  def reserved_rooms
    rooms.reserved_count
  end

  def free_rooms
    rooms.free_count
  end
end
<table>
  <tr>
    <th>Name</th>
    <th>Rooms count</th>
    <th>Rooms status: in reserved || free</th>    
  </tr>

  <% @hotels.each do |hotel| %>
    <tr>
      <td><%= hotel.name %></td>
      <td><%= hotel.rooms_count %></td>
      <td><%= "#{hotel.reserved_rooms} || #{hotel.free_rooms}" %></td>      
      <td ><%= link_to 'Show', hotel_path(hotel) %></td>
      <td><%= link_to 'Destroy', hotel, method: :delete, data: { confirm: 'Are you sure?' } %>
    </tr>
  <% end %>
</table>
@hotels = Hotel.includes(:rooms).sort_by { |h| h.free_rooms.to_i }.reverse