Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:09:25+00:00 2026-06-03T10:09:25+00:00

I’m new to Ruby and Rails (and programming!), and trying to figure out the

  • 0

I’m new to Ruby and Rails (and programming!), and trying to figure out the idiomatic way of passing properties from a model to its STI children.

I have a generic model ‘Document’, and some models that inherit from it — let’s take ‘Tutorial’ as the example. I have a string field for an ‘icon’, in which I want to store the icon’s filename but not full path (I think the path should be up to each model, as it’s a detail of retrieving the record’s data?):

class Document < ActiveRecord::Base
  attr_accessible :title, :body, :icon

  @@asset_domain = "http://assets.example.com/"
  @@asset_path = "documents/"

  def icon
    @@asset_domain.to_s + @@asset_path.to_s + read_attribute(:icon).to_s
  end
end

This is the kind of thing I’d like to do with the subclasses, so they look for their ‘icons’ (or any other asset) in an appropriate place.

class Tutorial < Document
  attr_accessible :title, :body, :icon

  @@asset_path = "tutorials/"

  # Other tutorial-only stuff
end

I’ve read about class variables and understand why what I’ve written above didn’t work quite as I intended, but what’s the best way of overriding ‘asset_path’ in the Tutorial class? I don’t think I should use instance variables as the values don’t need to change per instance of the model. Any ideas much appreciated (even if it means rethinking it!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:09:33+00:00Added an answer on June 3, 2026 at 10:09 am

    It looks like you are trying to create a constant value that you can reuse to build paths. Instead of using a class variable, I would use a constant.

    Now the question of placement:

    In-Class

    If it really only needs to be used in Document and the classes that inherit from it, define a constant at the top of the stack:

    # document.rb
    #
    class Document < ActiveRecord::Base
      attr_accessible :title, :body, :icon
    
      ASSET_DOMAIN = "http://assets.example.com/"
    
    end
    

    This would be accessible in Document Tutorial and other objects that inherit from those.

    Environment.rb

    If this is a value you are going to use everywhere, what about adding a constant to your environment.rb? That way you don’t have to remember to redefine it in all the classes you placed it.

    # environment.rb
    #
    # other config info
    #
    ASSET_DOMAIN = "http://assets.example.com/"
    

    And then you could build links wherever you like and not be constrained by Class:

    # documents.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    
    # tutorial.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    
    # non_document_model.rb
    #
    icon_path = ASSET_DOMAIN + path_and_file
    

    This may be editorializing, but rubyists seem to cringe when they see @@. There’s a time and place, but for the kind of thing you want to do I would use a constant and decide where you need to place it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.