Newbie question

Take this code:

<% for news in @news %>

<%= news.title %>

<%=h news.date.strftime("%A, %B %d, %Y ") %>

<%= news.copy %>

<% end %>

OOPS!

Take this code:

<% for news in @news %>

<%= news.title %>

<%=h news.date.strftime("%A, %B %d, %Y ") %>

<%= news.copy %>

<% end %>

What if I just wanted the most recent news item to be used instead of
ALL of them.

Sorry, this is a dumb question but i’m new!

mk

In the controller that calls the view add:

@news = News.find(:first, :order => “date DESC”)

so @news will only contain the latest record.

Mason K. wrote:

OOPS!

Take this code:

<% for news in @news %>

<%= news.title %>

<%=h news.date.strftime("%A, %B %d, %Y ") %>

<%= news.copy %>

<% end %>

What if I just wanted the most recent news item to be used instead of
ALL of them.

Sorry, this is a dumb question but i’m new!

mk

This is so simple. And really helps me to see the whole MCV idea. I’m
just a simple designer who is pretty much new to the development gig.

If you don’t mind my asking?

How would I be able to call up

a) the first two items in news
b) the first and the third item in news
c) the first and the last item in news?

Thanks for your help!

François Montel wrote:

In the controller that calls the view add:

@news = News.find(:first, :order => “date DESC”)

so @news will only contain the latest record.

Mason K. wrote:

OOPS!

Take this code:

<% for news in @news %>

<%= news.title %>

<%=h news.date.strftime("%A, %B %d, %Y ") %>

<%= news.copy %>

<% end %>

What if I just wanted the most recent news item to be used instead of
ALL of them.

Sorry, this is a dumb question but i’m new!

mk

On 7/25/06, Mason K. [email protected] wrote:

This is so simple. And really helps me to see the whole MCV idea. I’m
just a simple designer who is pretty much new to the development gig.

If you don’t mind my asking?

How would I be able to call up

a) the first two items in news

b) the first and the third item in news

c) the first and the last item in news?

I’m a newbie too, and there are experts on this mailing list who can
give
you much better solutions than I, but off the top of my head, you could
create arrays for options a) b) and c) in your controller, and then just
show them in your view. For example:

in your controller:

@news = News.find(:all, :order => ‘date ASC’)
@first_two_news = [ @news[0], @news[1] ]
@first_and_third_news = [ @news[0], @news[2] ]
@first_and_last_news = [ @news[0], @news[@news.length-1] ]

Note: @news is a collection of objects, and collections and arrays in
Ruby
start with 0 instead of 1. That’s why @news(0) is the first record in
the
array. The last record would be @news.length (the number of records in
@news) minus 1 (since you’re starting with 0).

Alternatively, you can just have the first line above in your
controller,
and then in your view you could have an IF statement that would evaluate
each record as you process it, and then do something or not do something
with it accordingly.