Q:

Using has_one correctly

I have a gallery model which has_many :photos
so far so good. 
I want each gallery to have its own feature_photo. my first instinct was to do 
class Gallery 
  has_one :feature_photo

so I would be able to do @gallery.feature_photo and I would save the photo id of each feature photo in their gallery models by having a feature_photo_id
Now I see that I should use a belongs_to, but semantically its confusing since a gallery doesnt belong to a photo
Now I am totally confused.. 
Should I use has_one or belongs_to and how can I use has_many :photos along with feature_photo in a gallery?

A:

belongs_to is used when the child object doesn't exist without the parent model. In your case, the feature_photo needs to be related to a gallery and it can't exist independently. Hence using has_one makes sense. If you are using mongodb, you can use the embeds_one relationship. Refer http://two.mongoid.org/docs/relations/embedded/1-1.html
In the above scenario, Gallery has_many Photos and Gallery has_one feature_photo. You can handle this in two way.
1) Add a boolean field in photos table denoting whether it is a feature photo or not. the problem with this approach that only one photo is going have a 'true' value for the field. If you have a large collection of photos, you are going to waste a lot of space for saving true/false value.
2) Add a feature_photo_id field in gallery and store the id of a photo. Write a method to retrieve the feature_photo. The problem with this approach is that you have to write all the logic yourself.
class Gallery 
  has_many :photos

  def feature_photo
     photos.where(id: feature_photo_id).first
  end
end

I would prefer the second approach.

