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

  • Home
  • SEARCH
  • 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 6576923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:34:18+00:00 2026-05-25T15:34:18+00:00

Say I have a table defined such that: Entities has_many Images Images has_one Entity

  • 0

Say I have a table defined such that:

Entities has_many Images  
Images has_one Entity

I have a record in the Images table that has no value for entity_id that I would like to be the default image for any new entity.

When I use:

@entities.images.find(:all, :conditions => ['image_type = ?', 'avatar])

I would like the default record from the Images table to be returned iff there is no image for the current entity

Is this possible, and if so, how?

Thanks.

  • 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-05-25T15:34:18+00:00Added an answer on May 25, 2026 at 3:34 pm

    You can create a method for this in your Entity model (best practice is to use singular names for models.)

    class Entity
    
      def avatar_images
        avatars = images.find(:all, :conditions => ['image_type = ?', 'avatar'])
        if avatars.empty?
          avatars = Image.find(:all, :conditions =>
            ['entity_id is null and image_type = ?', 'avatar'])
        end
        avatars
      end
    
    end
    

    Then just call that method in your controller to get either the avatar images belonging to the entity, or the default images if it has none.

    @avatars = @entity.avatar_images
    

    Edit:

    It is probably a good idea to generalize this method to accept an image type as parameter, to make it more versatile:

    class Entity
    
      def images_of_type(type)
        list = images.find(:all, :conditions => ['image_type = ?', type])
        if list.empty?
          list = Image.find(:all, :conditions =>
            ['entity_id is null and image_type = ?', type])
        end
        list
      end
    
    end
    

    Then in your controller:

    @avatars = @entity.images_of_type('avatar')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table (lets say it has one column called 'colLanguage') that contains
Let's say we have a simple table defined as: <p:dataTable value=#{testBean.dummyStringData} var=data> <p:column> <p:commandLink
Lets say I have table with ID int, VALUE string: ID | VALUE 1
Say I have a table that goes clockwise like this: 2 1 3 0
I have a table say, ITEM, in MySQL that stores data as follows: ID
Let's say I have a user defined table type: create or replace type SrcCodeTbl
Lets say I have some object that I've defined elsewhere. Maybe it represents a
let's say I have a column on my table defined the following: MyColumn smallint
I've noticed something odd about user-defined variables: Lets say I have this table: num_table
Say I have an entity that will have many attributes, some I know about

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.