SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ActiveRecord Issues - Uninitialized Contant

    Hi,

    Just having a play around with RoR for a few little automation tasks and it's doing my nut in right now. I've got two models:

    Item:
    Code:
    class Item < ActiveRecord::Base
      belongs_to :something
    end
    Something:
    Code:
    class SomeThing < ActiveRecord::Base
        has_many :items
    end
    The table for items has: something_id column in it.

    Yet when I try:
    Code:
    <%=h item.something.value %>
    I get:
    Code:
    uninitialized constant Item::Something
    and it's doing my head in right now as to why it isn't working.

    It's bound to be something simple that I'm just not seeing, having come from many years with PHP.

    Cheers,
    Last edited by Karl; Nov 4, 2010 at 03:41.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  2. #2
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is that exactly the code you have? because you'd need to actually specify the name of the correct model (e.g. in the case you've provided you'd need to say belongs_to :rate and not belongs_to :something).

    If the name of the class is different from the name of the relation you want to use, you need to specify :class_name in your belongs_to declaration.

  3. #3
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sorry, that should have been class Something not class Rate, that swhat I get for looking at two different tinges when I'm posting.

    Thanks,
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  4. #4
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So, do you mean you've got it working now, or do you just mean that your initial query should have had that class name?

  5. #5
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No, it was a typo in my original post. The models were a little more complex than as posted, but I've stripped them down to what you see now in my original (edited) post and it's having non of it.

    Thanks,
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  6. #6
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cool,

    Now I'm seeing one more potential issue: you have class SomeThing camel-cased. If that's indeed the case, you should be using item.some_thing and not item.something ... Rails automatically inflects your camel-cased names for use in file names and database tables, and that's also reflected in the accessor methods generated by belongs_to or has_many.

  7. #7
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I did actually try that before, so item looks like this:
    Code:
    class Item < ActiveRecord::Base
      belongs_to :some_thing
    end
    Now that just gives me a different error:

    Code:
    undefined method `value' for nil:NilClass
    When doing:

    Code:
    <%=h item.some_thing.value %>
    value does exist, as you can see from the schema:

    Code:
      create_table "some_things", :force => true do |t|
        t.integer  "value"
        t.string   "name"
        t.string   "description"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
    Thanks,
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  8. #8
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cool. Believe it or not, that's progress

    At this point the method some_thing of your item isn't throwing an error like it was before, so your association is set up correctly. However, you're getting nil when retrieving the some_thing associated with that particular item, which would imply that it doesn't have one. Try creating a new item and make sure there's a some_thing associated with it. If you're looping over a list of items, and only some of them will have some_things, you'll need to check to see if they're there before trying to get their properties:
    Code Ruby:
    if item.some_thing
      item.some_thing.value
    end


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
  •