So I was implementing a feature that allows users to set default profile photos and default photo album cover photos.
Completed these features now:
I have the following tables in my database:
User(s) (has one profile, has many PhotoAlbums)
Profile(s) (belongs to user) contains photo_id
PhotoAlbum(s) (belongs to user, has many photos) contains photo_id
Photo(s) (belongs to PhotoAlbum)
Displaying the users default photo is straight forward.
I’m having trouble displaying the users default album photo now.
This is how I display users default profile photo:
UsersController:
default_profile_photo_id = current_user.profile.photo_id
@default_photo = Photo.find(default_profile_photo_id)
Users view show template:
<%= link_to image_tag(@default_photo.image, :width =>"200"), @user.username %>
Currently for photoalbums default photos I show the first photo inside that photo album:
<% for photo_album in @photoalbums %>
<%= link_to photo_album.album_title, photo_album %>
<%= link_to image_tag(photo_album.photos.first.image, :width =>"100"), photo_album %>
<%= link_to "Edit Album", edit_photo_album_path(photo_album) %>
<%= photo_album.photos.size %>
<% end %>
Associated controller action:
def index
@photoalbums = PhotoAlbum.find_all_by_user_id(current_user.id)
end
What I need to do is during the for loop, pull the row from the photos table that has an id that matches the photo_id of the photoalbum that the loop is currently looping so that on my index template I can pull the image from the matching photo row and display it.
I’ve tried endless solutions but none work..
I feel I need a helping hand from an experienced programmer.
Kind regards
PhotoAlbum model:
view:
Very, very simple fix inspired by Frederick Cheungs comment.