开发者

find a random entry from an association in rails

开发者 https://www.devze.com 2023-01-06 16:33 出处:网络
Controller: class GalleriesController < ApplicationController def index @galleries = Gallery.all end end

Controller:

class GalleriesController < ApplicationController
  def index
    @galleries = Gallery.all
  end
end

View:

<% for gallery in @galleries %>
  <%= image_t开发者_运维技巧ag(gallery.image.url(:medium)) %>
<% end %>

I have 2 models, Photo which belongs to Gallery,which has many Photos. I want to display an image (preferably random rather than :first) from each gallery on the gallery index page. I know the code I have is wrong as I haven't described which image to select from the gallery, but I'm not sure how to do this... I thought that using @photos = @galleries.photos.find(:first) would work but I get an undefined method 'photos' in the controller.


This works in Rails 2 + MySQL

class Photos < ActiveRecord::Base
  # for MySQL:
  named_scope :random, lambda { |n| {:order => "RAND()", :limit => n || 1 }}
  # for SQLite and PostgreSQL
  named_scope :random, lambda { |n| {:order => "RANDOM()", :limit => n || 1 }}
end

Then you can do something like

gallery.photos.random[0]

gallery.photos.random will return 1 Photo randomly, but it will still return an array, that's why you need the [0] to get the first element of this array. you can also do gallery.photos.random.first.

If you want more than one random image you can call gallery.photos.random(10) to get 10 random photos.


@galleries is an array so you can't write @galleries.photos.find(:first) but you have to write @galleries[0].photos.find(:first)

for random order try following

<% for gallery in @galleries %>

<%= image_tag(gallery.photos.order('rand()')) %>


You are asking the @galleries array for its photos which will not work. You need to ask an instance of Gallery for its photos.

So within your for loop then the following should work for you

<% for gallery in @galleries %>
<%= image_tag(gallery.photos.first.image.url(:medium)) %>

If you want to get one photo in Random order then you could adpot Salils suggestion or perhaps add a method to the Gallery model as follows :-

def random_image()
  photos.all[rand(photos.all.size)]
end

And then within your foreach loop just do <%= image_tag(gallery.random_image) %>

0

精彩评论

暂无评论...
验证码 换一张
取 消