#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    2
    Rep Power
    0

    Model find_by_column_name only returns 1 result


    If i go

    stories = Story.find_by_genre_id(8)

    The model retrieves the first record with genre_id = 8...why doesnt it return all the stories with genre_id = 8?

    Note:

    Code:
    class Genre < ActiveRecord::Base
    has_many :stories
    end
    
    class Story < ActiveRecord::Base
    belongs_to :genre
    end
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    Yup, that's how it is designed. If you know you have more than one record, use find_all_by_genre_id() instead of find_by_genre_id()
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    intricate evolution
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    ft. lauderdale, FL
    Posts
    315
    Rep Power
    11
    im just starting out with RoR, but what about

    stories = Genre.find_by_id(8).stories
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    Originally Posted by intrktevo
    im just starting out with RoR, but what about

    stories = Genre.find_by_id(8).stories
    That almost works, just use find() instead of find_by_id().

    EDIT:
    Actually, that works too! Sorry.
    Last edited by destin; May 29th, 2008 at 07:36 AM.

IMN logo majestic logo threadwatch logo seochat tools logo