레일즈 3의 랜덤 항목에 대한 질문이 있습니다.레일즈 : 여러 범주 내에서 임의의 제품 가져 오기
class Product < ActiveRecord::Base
belongs_to :category
self.random
Product.find :first, :offset => (Product.count * ActiveSupport::SecureRandom.random_number).to_i
end
end
class Category < ActiveRecord::Base
has_many :products
end
임의의 오프셋 캐스팅을 사용하여 모든 제품에서 임의의 제품을 int로 가져올 수 있습니다. 그러나 나는 또한 몇몇 주어진 카테고리 내에서 임의의 제품을 얻을 수 있기를 원한다. 나는 이런 식으로 시도했다. 그러나 이것은 오프셋 인덱스 때문에 작동하지 않는다.
class Product < ActiveRecord::Base
belongs_to :category
self.random cat=["Mac", "Windows"]
joins(:categories).where(:categories => { :name => cat }).where(:first, :offset => (Product.count * ActiveSupport::SecureRandom.random_number).to_i)
end
end
누가 더 나은 해결책을 알고 있는가?
thx! tux
답장은 Thx입니다. 내가 제품과 카테고리의 거대한 목록을 가지고 있기 때문에 나는 rand()에 의한 주문을 벤치마킹하고 있었고, 그것은 나를 위해 작동하지 않는다. 그래서 그것은 rand()에 의해 너무 느릴 것입니다. 그러나 self.count는 나를 위해 일한다! 고마워! – 23tux