diff --git a/lib/jam_ruby/models/band.rb b/lib/jam_ruby/models/band.rb index 2f6a3b635..965f49c27 100644 --- a/lib/jam_ruby/models/band.rb +++ b/lib/jam_ruby/models/band.rb @@ -1,6 +1,8 @@ module JamRuby class Band < ActiveRecord::Base + attr_accessible :name, :website, :biography + self.primary_key = 'id' # musicians @@ -10,6 +12,8 @@ module JamRuby # genres has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "bands_genres" + after_save :limit_to_three_genres + def photo_url # TODO: move image path to config @photo_url = "http://www.jamkazam.com/images/bands/photos/#{self.id}.gif" @@ -22,7 +26,53 @@ module JamRuby # helper method for creating / updating a Band def self.save(params) + if params[:id].nil? + band = Band.new() + else + band = Band.find(params[:id]) + end + + # name + unless params[:name].nil? + band.name = params[:name] + end + + # name + unless params[:website].nil? + band.website = params[:website] + end + + # name + unless params[:biography].nil? + band.biography = params[:biography] + end + + # genres + genres = params[:genres] + unless genres.nil? + ActiveRecord::Base.transaction do + # delete all genres for this band first + unless band.id.nil? || band.id.length == 0 + band.genres.delete_all + end + + # loop through each instrument in the array and save to the db + genres.each do |genre_id| + g = Genre.find(genre_id) + band.genres << g + end + end + end + + band.updated_at = Time.now.getutc + band.save + return band end + def limit_to_three_genres + if self.genres.count > 3 + errors.add(:genres, "No more than 3 genres are allowed.") + end + end end end \ No newline at end of file diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index 7c1dbdb5b..57e16523c 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -29,6 +29,8 @@ module JamRuby has_many :friends, :through => :friendships, :class_name => "JamRuby::User" has_many :inverse_friendships, :class_name => "JamRuby::Friendship", :foreign_key => "friend_id" has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :class_name => "JamRuby::User" + + # connections / music sessions has_many :created_music_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::MusicSession" # sessions *created* by the user has_many :music_sessions, :through => :connections, :class_name => "JamRuby::MusicSession" @@ -113,15 +115,16 @@ module JamRuby unless params[:instruments].nil? ActiveRecord::Base.transaction do # delete all instruments for this user first - MusicianInstrument.delete_all(["user_id = ?", user.id]) + unless user.id.nil? || user.id.length == 0 + MusicianInstrument.delete_all(["user_id = ?", user.id]) + end # loop through each instrument in the array and save to the db params[:instruments].each do |instrument| mu = MusicianInstrument.new() mu.user_id = user.id - puts "INSTRUMENT ID #{instrument[:id]}" mu.instrument_id = instrument[:id] - puts "proficiency level #{instrument[:proficiency_level]}" + mu.priority = instrument[:priority] mu.proficiency_level = instrument[:proficiency_level] user.musician_instruments << mu end