SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 7 NoMethodError: (Ruby)

    Hi all-

    i'm stuck in chapter 7, p192. when i enter
    Code:
    v.story
    i get this error:

    NoMethodError: undefined method `story' for #<Vote:0x31a6488>
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'

    here's my schema:
    # This file is autogenerated. Instead of editing this file, please use the
    # migrations feature of ActiveRecord to incrementally modify your database, and
    # then regenerate this schema definition.

    ActiveRecord::Schema.defineversion => 3) do

    create_table "stories", :force => true do |t|
    t.column "name", :string
    t.column "link", :string
    t.column "permalink", :string
    end

    create_table "votes", :force => true do |t|
    t.column "story_id", :integer
    t.column "created_at", :datetime
    end

    end


    vote.rb:
    class Vote < ActiveRecord::Base
    belongs_to :story
    end


    and story.rb:
    class Story < ActiveRecord::Base
    validates_presence_of :name, :link
    has_many :votes
    end


    thank you!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You code is correct. Maybe you need to reload your models? (by calling reload! in the console).

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fenrir2- that totally worked. how would one know when the models need to be reloaded? and what exactly does reload do?

    and THANK YOU.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to reload if you make changes to your model files (in app/models) while you are using the console. Note that you don't have to reload if you close the console and start a new one: all models are loaded automatically when you start up.

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Location
    Denmark.
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very interesting.
    At Which console and path should the reload be executed?
    Where can I myself find the answer to this question?

    Thank you in advance

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Reload should be executed in the rails console. You start it with `ruby script/console`. reload!() is a Ruby/Rails method, so you can only call it inside the Rails console (which is a special Ruby console).

    I think you can find this information if you search for reload! on railsmanual.org

    Oh, and you also have to call reload! if you change the db schema.

  7. #7
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is good to know. I'm getting this problem in Chapter 8 in the unit tests. It seems to reload my development database, but not my test database? Perhaps a different argument?

  8. #8
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Turns out I had failed to make the connection to the database within the model. hth someone, sam


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
  •