How to List Records in Descending Order?


#1

I am brand-new to RoR and have only followed and replicated the few
tutorials that are out there.

I have searched and searched, but cannot find an answer to this simple
question:

How can you list records in descending order?

I have the cookbook example loaded, but can’t seem to list the recipes
in reverse order (by the primary key - id).

Thanks ahead of time for any and all help.


#2

Hi Geo, you can do the following:

model_name.find( :all, :order => “id DESC” )

Good luck,

-Conrad


#3

Conrad T. wrote:

Hi Geo, you can do the following:

model_name.find( :all, :order => “id DESC” )

Good luck,

-Conrad

Thanks, but where do I put that line of code? Model? Controller? View?

And where was model_name defined?


#4

Conrad T. wrote:

Hi, you would put this snippet of code within a controller’s action.

Good luck,

-Conrad

If I put that line in the controller, I get the following error:

undefined local variable or method `model_name’ for
RecipeController:Class


#5

You need to change “model_name” to your model’s name (e.g., Recipe or
whatever it’s called). :slight_smile:

I suggest reading some tutorials about the basics of Rails before
jumping into a Cookbook…it would help with a lot of frustrations.

–Jeremy

On 4/1/07, Geo P. removed_email_address@domain.invalid wrote:

undefined local variable or method `model_name’ for
RecipeController:Class


Posted via http://www.ruby-forum.com/.


http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/


#6

Hi, you would put this snippet of code within a controller’s action.

Good luck,

-Conrad


#7

Hi Geo, whenever someone provides code like model_name, it shouldn’t
not be implied as the actual model name. Thus, model_name in your
context is whatever model you’re using to retrieve the data. In any
case, I agree with Jeremy that you should get an understanding of the
basics of Rails and you can do that by going through the “Agile Web
Development with Rails 2 Edition” (AWDwRv2) book. Finally, RoR gets
much easier when you have learned the basics and this translates into
increased productivity.

-Conrad


#8

Jeremy McAnally wrote:

You need to change “model_name” to your model’s name (e.g., Recipe or
whatever it’s called). :slight_smile:

That is what I was thinking, but the poster didn’t state that nor did
they post it in a way that signified that. For example, the following
is the “normal” way that I am used to seeing something like that:

<model_name>.find( :all, :order => “id DESC” )

Also, since RoR hides most of the “code” behind the scenes, it is hard
to know exactly what has been defined and what hasn’t.

I suggest reading some tutorials about the basics of Rails before
jumping into a Cookbook…it would help with a lot of frustrations.

–Jeremy

There don’t seem to be many tutorials on the web (at least that I could
find), but I have read every tutorial that I could find (multiple
times). Do you have any that you recommend?

I have also searched multiple forums to no avail. That is why I am
posting the question here.

I felt that this should be a relatively easy thing to do, but have spent
way too much time on this (especially when people tout how easy RoR is).


#9

Hi Geo, the point of this mailing list is to help people answer and
solve development issues. However, it seems that you would benefit
yourself by simply learning the basics so that when someone provides
an answer you can understand what we are talking about. Next, in your
originally e-mail, you never provided any code so that we can properly
assist you. You simply ask the following question:

How can you list records in descending order?

Furthermore, this told me nothing about the controller and/or model
name within your Rails application. Also, you didn’t provide a link
to the site that you were using to build your application. Thus, the
more information that you provide in regards to your issue the better
that we can assist you here. OK?

-Conrad


#10

So then what’s the use of this forum (if not to ask basic questions)?
Why not just close down the forums and put up a web page that says “Go
Read the Book”?

While I appreciate you taking the time to answer my question, it was
only partially-answered. You gave me a snippet of code without telling
me where it was supposed to go. On top of that, some of the “code”
wasn’t even code.

And finally, even if I replace model_name with Recipe, it still doesn’t
work.


#11

To get this post back on topic I would make the suggestion that the
logic of descending the data be a method in your model for example:

NOTE: this code sample uses Recipe as the model name.

def self.find_recipes
find(:all, :order => “id DESC” )
end

Then in your controller when you are listing the recipes you would
just call:

@recipes = Recipe.find_recipes

Again, replace with your desired model and variable names.

Just my two cents,

  • Blaine

#12

Yes, Geo - the Model being were all your data logic should go. It’s
the same thing, just more expandable later because anything using the
models logic would be changed by just changing the model.

  • Blaine

On Apr 2, 7:17 am, Geo P. removed_email_address@domain.invalid


#13

Conrad T. wrote:

…you never provided any code so that we can properly
assist you. You simply ask the following question:

How can you list records in descending order?

Furthermore, this told me nothing about the controller and/or model
name within your Rails application. Also, you didn’t provide a link
to the site that you were using to build your application. Thus, the
more information that you provide in regards to your issue the better
that we can assist you here. OK?

Conrad:

Thanks for your response.

I thought that I did provide enough information. In my original email,
I stated “I have the cookbook example loaded, but can’t seem to list the
recipes in reverse order (by the primary key - id).”

I figured that everyone has the cookbook example, so didn’t think that I
needed to provide any code snippets. Next time I will.

Thanks for your help. I have been able to take your pseudo-code snippet
and figure out the rest.

blaine:

Thanks for your suggestion. It look like another way of doing the same
thing that Conrad was suggesting, except it defines another method in
the model.