# Number limited redirects

**URL:** <https://www.ruby-forum.com/t/number-limited-redirects/181089>\
**Category:** Rails\
**Created:** [January 5, 2010, 10:42am UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089 "2010-01-05T10:42:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 10:42am UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/1 "2010-01-05T10:42:30Z")

</div>

The following is simply for testing and educational purposes:

def new  
if request.get?  
@album = Album.new(:artist\_id =\> params[:artist])  
else  
@album = Album.new(params[:album])  
if @album.save  
flash[:notice] = “Album successfully added. Add a Song”  
redirect\_to :controller =\> ‘song\_admin’, :action =\> ‘new’,  
:album =\> @album  
else  
flash[:notice] = “Failed to add Album”  
flash[:error\_field] = :album  
end  
end  
end

Once the album is saved, it redirects to allow the user to add a song to  
the album. On the album form, I have a field: “no\_songs” as an integer.  
I want the redirect to happen as many times as specified in the  
“no\_songs” column.

I’m not sure what syntax to use here.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 4:02pm UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/2 "2010-01-05T16:02:13Z")

</div>

Do you mean that you only want to redirect if the number of songs in the  
album is less than no\_songs? If so then just test album.songs.size  
against params[:no\_songs].

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 4:52pm UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/3 "2010-01-05T16:52:24Z")

</div>

Max W. wrote:

> Do you mean that you only want to redirect if the number of songs in the  
> album is less than no\_songs? If so then just test album.songs.size  
> against params[:no\_songs].

Thanks for your reply but what I meant was, I want the redirect to occur  
as many times as is entered in the ‘no\_songs’ column.

For example: I add an album and set ‘no\_songs’ to 7. Upon update, it  
redirects me to add a song to the album using parameters. I only want  
this redirect to occur 7 times as I’ve told the system that there are  
only 7 songs in the album.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/4 "2010-01-05T16:57:54Z")

</div>

Pale H. wrote:

> Max W. wrote:
> 
> > Do you mean that you only want to redirect if the number of songs in the  
> > album is less than no\_songs? If so then just test album.songs.size  
> > against params[:no\_songs].
> 
> Thanks for your reply but what I meant was, I want the redirect to occur  
> as many times as is entered in the ‘no\_songs’ column.
> 
> For example: I add an album and set ‘no\_songs’ to 7. Upon update, it  
> redirects me to add a song to the album using parameters. I only want  
> this redirect to occur 7 times as I’ve told the system that there are  
> only 7 songs in the album.

Well, you could add a countup value to a hidden field and then in the  
controller set an instance variable from it and test if it’s \> 7. If it  
is then that’s all the attempts used up, if not then increment it and  
re-render the page, setting the field from the variable.

This is kind of a horrible way of doing things, why not just put space  
for seven (or whatever) songs on the form in the first place? I know  
you said it’s just an example, i just had to mention that…

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/5 "2010-01-05T17:08:00Z")

</div>

Pale H. wrote:

> > This is kind of a horrible way of doing things, why not just put space  
> > for seven (or whatever) songs on the form in the first place? I know  
> > you said it’s just an example, i just had to mention that…
> 
> I know what you mean and I agree with you but I’m doing it this way to  
> teach myself associations so I have separate song, album and artist  
> tables. I’m picking it up easy enough, I guess.

This doesn’t really teach you about associations though, it just teaches  
you how to do a really horrible application design 🙂

A more useful way to use the associations is to look at the extra  
methods given to you by the associations, which allow you to add seven  
songs to an album at once for example.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 6, 2010, 10:39am UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/6 "2010-01-06T10:39:09Z")

</div>

> This doesn’t really teach you about associations though, it just teaches  
> you how to do a really horrible application design 🙂
> 
> A more useful way to use the associations is to look at the extra  
> methods given to you by the associations, which allow you to add seven  
> songs to an album at once for example.

Well, making tables that are associated together surely does teach me  
about associations and use of primary keys…

I know what you mean about using more appropriate methods to do this, I  
was just querying that particular method. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 5, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/number-limited-redirects/181089/7 "2010-01-05T17:05:06Z")

</div>

> This is kind of a horrible way of doing things, why not just put space  
> for seven (or whatever) songs on the form in the first place? I know  
> you said it’s just an example, i just had to mention that…

I know what you mean and I agree with you but I’m doing it this way to  
teach myself associations so I have separate song, album and artist  
tables. I’m picking it up easy enough, I guess.
