Albums
<% @albums.each do |a| %>
- <%= check_box_tag 'photo[album_ids][]', a.id, @photo.albums.include?(a) %>
<%= hidden_field_tag 'photo[album_ids][]', '' %>
<%= f.label :id, a.title %>
<% end -%>
------------------------------------------------------------------------------
after_save :update_albums
def album_ids=(ids)
if new_record?
@new_album_ids = ids.reject(&:blank?)
else
self.albumizations.each { |z| z.destroy unless ids.delete(z.album_id.to_s) }
ids.reject(&:blank?).each {|id| self.albumizations.create(:album_id => id) }
end
end
def album_ids
new_record? ? @new_album_ids : albums.map(&:id)
end
private
def update_albums
self.album_ids = @new_album_ids if @new_album_ids
end
-------------------------------------------------------------------------------
<% content_for :secondary do %>
Albums
<% @albums.each do |album| %>
-
<%= link_to album.title, album %>
<%= link_to image_tag(album.photos.last.image.url(:square)), album if album.photos.last %>
<% end -%>
<% end -%>
-------------------------------------------------------------------------------
def next(album=nil)
collection = album ? album.photos : self.class.find(:all)
collection.size == collection.index(self) + 1 ? collection.first : collection[ collection.index(self) + 1 ]
end
def previous(album=nil)
collection = album ? album.photos : self.class.find(:all)
collection[ collection.index(self) - 1 ]
end