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 8290683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:52:50+00:00 2026-06-08T12:52:50+00:00

I have a Picture class and I want to override returning the description database

  • 0

I have a Picture class and I want to override returning the “description” database column value depending on some logic.

Is this valid in Rails? Can I always rely on the Class to call the class method before returning the database column value?

# database columns for Picture class
# description => String
# parent_id => Integer

class Picture < ActiveRecord::Base

  def description
    if parent_id.nil?
      self.description      
    else
      description = Picture.find(parent_id).description
    end
  end

end 

Couldn’t figure out where to find the answer in the Rails source code so any help would be appreciated.

Thanks!

EDIT

I’m trying to return a Picture’s description depending on whether or not it’s a child or parent Picture (they can be nested). The example is a bit contrived…I could easily avoid this by using a method without the same name as the database column…such as

def my_description
   if parent_id.nil? # return db data since no parent
      self.description
   else # return parent's description if one exists
      description = Picture.find(parent_id).description
   end
end

I guess I’m trying to be unnecessarily complicated here

  • 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-08T12:52:53+00:00Added an answer on June 8, 2026 at 12:52 pm

    It’s not totally clear what you’re trying to do, but you can use super to get the description value from within the method:

    def description
      if parent_id.nil?
        super
      else
        self.description = Picture.find(parent_id).description
      end
    end
    

    Note that I changed the last line to use self.description = because I’m assuming you want to set the description value. If that’s not the case, then you don’t need to assign it to any variable.

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

Sidebar

Related Questions

I have class and I want to initalize column that stores pictuer with type
I my project I have a class that in this class I want to
I my project I have a class that in this class I want to
have a picture in my server named /images/pic.jpg . I want to track ip
I have a picture in res/drawable directory: res/drawable/picture.jpeg. Can I dynamically update this picture.jpeg
I have 3x3 picture boxes on a form, what I want to do is
What i want is that i have made a picture chooser, the image chosen
I have a dynamic imageview of camera picture, my problem is I want to
I have three Models: User, Picture, and Like where: class Picture include Mongoid::Document embeds_many
I have approximately the following picture: public class Foo { public Foo(Bar bar, String

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.