Reputation: 612
I have a two models Users and Photos.
Right now, users can have many photos. I'm looking for the the best rails/database pattern for saying they have one profile picture. Which is one of the photos they have.
User Model
class User < ActiveRecord::Base
attr_accessible :emailAddress, :firstName, :lastName, :middleName, :phoneNumber
has_many :photos
end
Photo Model
class Photo < ActiveRecord::Base
belongs_to :client
attr_accessible :image, :client_id
has_attached_file :image
end
Should I add a :profilePicture attribute to User that points to an existing photoId, or should I add a new model called ProfilePicture, with a belongs_to :client, has_one :photo?
Any suggestions or links that can help me figure out the best way to set this up would be helpful, thank you.
Upvotes: 1
Views: 1393
Reputation: 117
I made something similar where I called the profilePicture the avatar. the User model must have a avatar_id field in the table which is the photo_id for the pic used as the user's avatar. Note that the avatar_owner in a photo will only be assigned in photo instances that are avatar photos.
User Model
class User < ActiveRecord::Base
attr_accessible :emailAddress, :firstName, :lastName, :middleName, :phoneNumber, :avatar_id
has_many :photos
belongs_to :avatar, :class_name => "Photo", :foreign_key => "avatar_id"
end
Photo Model
class Photo < ActiveRecord::Base
belongs_to :client
attr_accessible :image, :client_id
has_attached_file :image
has_one :avatar_owner, :class_name => "User", :foreign_key => "avatar_id"
end
Upvotes: 2