I'm brand new to RoR and im loving it. I know how to fetch a single row from a database but how can i get it to get all listings.
Thanks to any one who replies![]()
| SitePoint Sponsor |

I'm brand new to RoR and im loving it. I know how to fetch a single row from a database but how can i get it to get all listings.
Thanks to any one who replies![]()

Thats what i had guessed it was but when i use it i get
I'm guessing i have somthing wrong with the view?Code:NoMethodError in News#list Showing app/views/news/list.rhtml where line #1 raised: undefined method `title' for #<Array:0x466c444> Extracted source (around line #1): 1: <strong><%= @news.title %></strong><br /> 2: <%= @news.body %>

Any one?
You're trying to get an instance variable of an object when that object is an array. You need to iterate through @news
Code:@news.each do |n| <strong><%= n.title %></strong><br /> <%= n.body %> end

Thanks Skyblaze and reynoldsdr you both helped me a lot![]()
I'm a little concerned that perhaps someone will come across this thread and not really get how you are explaining this.
With the model being named news I would do this in the controller:
In your view yes you need to loop through the array, however don't forget you need to use ERb Ruby Brackets.Code:@news = News.find(:all)
Just want to make sure all the instructions here are clear. But I'm glad you were already able to figure things out.Code:<% for news in @news -%> <strong><%= news.title %></strong><br /> <%= news.body %> <% end -%>

Ye i did work it out but thanks for the explination
I don't like using model names that pluralize ambiguously like News. Perhaps you should use something else like Article so you can more easily distinguish between collections (@articles) and single AR objects (@article). It should make your code easier to read later on.
VGarica, could you explain a little further? I don't how the difference between whether using "articles" or "news" matters in this case it just depends on the content in the model. The fact that we are using a pluralized instance variable shows we are pulling more then one - and rather then looping with an index of |i| or something random instead using of the singular (with news is still news) in the loop so in your example it would be "for article in @articles" - I don't see how that is any different in ease of readability. Granted I agree with that you any time something is only one item it should be @article.
Anyway, please explain a little further so I can understand what you me. I'm learning just like everyone else.




It technically doesn't matter, but it's often useful to know what a variable contains -- one object or a collection of objects -- by looking at the variable name. If you see @article you know it's one article. If you see @articles you know it's a collection of articles. If you see @news you don't know.
Agreed, I think I see the point he was making.



I agree vgarcia, say well away from names that don't pluralise. In fact in rails 1.2, you will run into issues if you use RESTful URLs with controllers that don't pluralise, and a rule of thumb I try and keep model and controller names consistent if they are to be used in pairs.
So avoid class names like ....
Sheep, Media, News ...
try and rename them to something more pluralisable, like MediaAsset (can be pluralized to media assets), or Article (pluralises to articles ) etc
Fenrir2 has a good point as well because you can do a better for of dealling with collection loops ....
e.g. for article in articles, you can't for for news in news unless it was for news in @news.
http://virtualfunction.net - Rails Web Development
http://squaremove.co.uk - Rails powered Property Listings
Bookmarks