开发者

Display if attribute is same as today's date

So I am working on an application where I want to display a video on the main page of the site. The video model has an actual_date and public_filename attribute. Now a use开发者_如何学编程r sets the actual date as :date in the model. I am trying to figure out how to best go about making the video on the homepage display the video that is assigned to today's date. My homepage view is:

Please wait while the video loads...

<script type='text/javascript'>
var s1 = new SWFObject('/flash/player.swf','player','500','372','9');
s1.addParam('allowfullscreen','true');
s1.addParam('allowscriptaccess','always');
s1.addParam('flashvars','file=<%= current_video.public_filename %>');
s1.write('preview');
</script>

I just included the current_video method, as I was playing around with creating one to return to the view with the right video url.

So I need a way to look at all of the videos in the database, see if any of the actual_date attributes are equal to today's date and if so tell current_video that it equals that video.

Thank you for any input or suggestions you might have!


You don't specify what should happen if more than one video has an actual_date that is equal to today's date. However, to retrieve one video just do:

@current_video = Video.first(:conditions => ['actual_date LIKE ?', Date.today])

—or you can do:

@current_video = Video.last(:conditions => ['actual_date LIKE ?', Date.today])

Whether you use first or last will depend on if you have some secondary sort order set up on your Video model. If it were me I'd create a named scope for this:

class Video < ActiveRecord::Base
  named_scope :for_today, :conditions => { 'actual_date LIKE ?', Date.today }
end

—then you can do

@video = Video.for_today
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜