# Simple banner / ad rotation on page (newbie question)

**URL:** <https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819>\
**Category:** Rails\
**Created:** [January 21, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819 "2007-01-21T13:30:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 21, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/1 "2007-01-21T13:30:54Z")

</div>

Hi,  
i try to figure out the solution for simple banner ad script. I’ve db  
table ‘banners’ with fields ‘id’, ‘image’, and ‘url’. I suppose to keep  
in this table 5 banners, so the ids are always from 1 to 5.  
I just wont to loop from 1 to 5 and show appropriate banner, in header  
layout (not view).

At this time i put the banner into layout’s header with direct query to  
db using Banner model:

_banner.rb_  
class Banner \< ActiveRecord::Base  
def self.banner  
@banner = Banner.find(:first, :conditions =\> [‘date\_end \> ?’,  
Time.now.strftime("%Y-%m-%d %H:%M:%S")])  
@banner.nil? ? “adimage.gif” : @banner.image  
end

_layout.rhtml_

\<%= image\_tag(Banner.banner) %\>

I think i must increment some variable in layout.rhtml and return this  
to Banner model.  
Or there is a better way?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 21, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/2 "2007-01-21T18:44:28Z")

</div>

On Jan 21, 2007, at 4:30 AM, ogurec wrote:

> query to  
> _layout.rhtml_
> 
> \<%= image\_tag(Banner.banner) %\>
> 
> I think i must increment some variable in layout.rhtml and return this  
> to Banner model.  
> Or there is a better way?

How about having your layout call a banner\_controller that handles  
the logic of picking which banner to present to the user?

## That seems like a more natural place to keep the logic, and a far better place to extend from later (view tracking, click tracking, whatever…)

Craig B.

AIM: kreiggers

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 21, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/3 "2007-01-21T19:38:37Z")

</div>

Can you give me some advice, how to do this?

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 22, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/4 "2007-01-22T19:15:13Z")

</div>

> > db usingBannermodel:
> > 
> > ## whatever…)
> > 
> > Craig B.
> > 
> > AIM: kreiggers
> > 
> > In my layout i call the method ‘banner’ of BannerController class:  
> > \<%= image\_tag(BannersController.banner) %\>
> > 
> > But how the controller “remembers” which banner is currently shown to  
> > user ?  
> > How or where is possible to store the id of last banner?

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 25, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/5 "2007-01-25T15:17:05Z")

</div>

any suggestion?

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 25, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/6 "2007-01-25T17:35:56Z")

</div>

define / require this in your environment.rb (or in lib or somewhere  
else):

class Counter

@@counter = 0

def self.nextValue  
@@counter = @@counter + 1  
end

end

in your action do this:

def my\_action  
@counter = Counter.nextValue  
end

now you can access the current counter value in your view

\<%= @counter %\>

This is sufficient for rotating banners _imho_

2007/1/25, ogurec [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 27, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/7 "2007-01-27T22:39:34Z")

</div>

If you use MySql (not sure RAND()) works with other DB) :

# In your model

class Banner \< ActiveRecord::Base  
…  
class \<\< self  
def random(limit=1)  
find(:all, :order =\> ‘RAND()’, :limit =\> limit)  
end  
end  
end

# In your view/controller

```
@banners = Banner.random(3)

```

Alain

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 25, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/8 "2007-01-25T18:31:04Z")

</div>

Ilja Ivanov wrote:

> I think i must increment some variable in layout.rhtml and return this  
> to Banner model.  
> Or there is a better way?

Just use rand() to select the banner to show. If you want different  
weights, there’s WeightedSelection  
([http://www.gemjack.com/gems/WeightedSelection-1.0.0/](http://www.gemjack.com/gems/WeightedSelection-1.0.0/)).

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 30, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/simple-banner-ad-rotation-on-page-newbie-question/82819/9 "2007-01-30T22:15:57Z")

</div>

First I assumed, that the quantity of images in table will be  
constant.  
But later the problem has become complicated ( for me as newbie 🙂 ),  
because I’ve added the field ‘date\_end’, so that banner can expire.  
It means, that the quantity of images varies, and a very simple  
looping through id in table does not give the solution.

Because as result of executing code  
@banners = Banner.find(@counter, :conditions =\> [‘date\_end \> ?’,  
Time.now.strftime("%Y-%m-%d %H:%M:%S")])  
i has error like ‘Could not find record with id = X and Date\_end \>  
Today’

I tried @banners.nil? and similar statements (blank, empty), but with  
no success. I think, this is because the error occures before the  
@banners became some value or nil at all.

As my first post says, banners table has fields ‘id’, ‘image’, and  
‘url’.

At this time process works this way:

in environment.rb i’ve added the Counter class as Peter E.  
suggested.  
class Counter  
@@counter = 0  
def self.increment\_cnt  
@@counter += 1  
end  
def self.toone\_cnt  
@@counter = 1  
end  
end

in controller that manages banners:  
def self.banner  
@counter = Counter.increment\_cnt  
@banners = Banner.find(:all,  
:conditions =\> [‘date\_end \> ?’, Time.now.strftime("%Y-%m-%d %H:%M:  
%S")])  
if @counter \> @banners.size then  
@counter = 1  
Counter.toone\_cnt  
end

[[@banners[@counter-1].image] , [@banners[@counter-1].url]]  
end

in layout  
\<% banner = BannersController.banner %\>  
\<%= link\_to image\_tag(banner[0].to\_s ), banner[1].to\_s %\>

So, this way i do not loop through ids of records in tables, but  
through elements of @banners array. Elements are in fact ActiveRecord  
objects, it doesn’t matter what ids they had
