SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails - Get all rows

    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

  2. #2
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by martian2k4 View Post
    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
    ModelName.find :all?

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats what i had guessed it was but when i use it i get

    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 %>
    I'm guessing i have somthing wrong with the view?

  4. #4
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any one?

  5. #5
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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><&#37;= n.title %></strong><br />
      <%= n.body %>
    end

  6. #6
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Skyblaze and reynoldsdr you both helped me a lot

  7. #7
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    @news = News.find(:all)
    In your view yes you need to loop through the array, however don't forget you need to use ERb Ruby Brackets.

    Code:
    <&#37; for news in @news -%>
      <strong><%= news.title %></strong><br />
      <%= news.body %>
    <% end -%>
    Just want to make sure all the instructions here are clear. But I'm glad you were already able to figure things out.

  8. #8
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ye i did work it out but thanks for the explination

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by slavenski View Post
    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:

    Code:
    @news = News.find(:all)
    In your view yes you need to loop through the array, however don't forget you need to use ERb Ruby Brackets.

    Code:
    <% for news in @news -%>
      <strong><%= news.title %></strong><br />
      <%= news.body %>
    <% end -%>
    Just want to make sure all the instructions here are clear. But I'm glad you were already able to figure things out.
    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.

  10. #10
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia View Post
    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.

  11. #11
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  12. #12
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed, I think I see the point he was making.

  13. #13
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •