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.
You can create a method for this in your
Entitymodel (best practice is to use singular names for models.)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.
Edit:
It is probably a good idea to generalize this method to accept an image type as parameter, to make it more versatile:
Then in your controller: