Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

VK

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Updating a join table by directly accessing the model

// project_controller.rb

def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
      
      for user in user_checkboxes :selected
        @project.update_assignment(user, params[:user_role][user.id.to_s])
      end
       
      for user in user_checkboxes :unselected
        @project.delete_assignment(user)
      end
      
      flash[:notice] = 'Project was successfully updated.'
      redirect_to :action => 'show', :id => @project
       
    else
      render :action => 'edit'
    end
  end

private

  def user_checkboxes(status)
    users = []
     for selected_user in params[:project_users].keys    
        choice = '1' if status == :selected
        choice = '0' if status == :unselected
        users << User.find(selected_user) if params[:project_users][selected_user] == choice
      end
    return users
  end 


// project.rb

class Project < ActiveRecord::Base
  has_many :assignments, :dependent => :destroy
  has_many :users, :through => :assignments
  has_many :roles, :through => :assignments

  def update_assignment(user, role_id)
    unless self.users.include?(user)
      Assignment.create(:project_id => self.id,
                        :user_id => user.id,
                        :role_id => role_id)
    else
      # update existing users
      assignment = Assignment.find_by_user_id_and_project_id(user.id, self.id)
      assignment.role_id = role_id
      assignment.save
    end
  end

  def delete_assignment(user)
    Assignment.destroy_all("user_id = #{user.id} AND project_id = #{self.id}")
  end

end

Validations with a join table

# I added this to the User Class:

attr_accessor :selected_roles

  def selected_roles
    @selected_roles
  end

  def validate
    errors.add_to_base("You must specify a role") if self.selected_roles.empty?
  end

# The create/update methods start like this:

 def create
    @user = User.new(params[:user])
    @user.selected_roles = get_selected_roles   
    if @user.save
      update_roles_users(@user.id)
      flash[:notice] = 'User was successfully created.'
      redirect_to :action => 'list'

# And a method for checking the HTML form:

def get_selected_roles
    selected_roles = Array.new
     for selected_role in params[:roles_user].keys
        # checkbox sends 1 if selected
        selected_roles << Role.find(selected_role) if params[:roles_user][selected_role] == "1" 
      end
      return selected_roles
  end

Print and preselect checkboxes

Create a selection box list and preselect with previously assigned Roles

def createCheckBoxList(user)
    
    userRolesID = Hash.new
    user.roles.each do | ur |
      userRolesID[ur.name] = ur.id   
    end
 
    @roles = Role.find(:all)
    
    html_output = String.new
    # first create the checkbox
      for role in @roles
        if userRolesID.has_key?(role.name)
          html_output << check_box("roles_user", role.id, :checked => 'checked')
        else
          html_output << check_box("roles_user", role.id)
        end
        # then print the role name
        html_output << " " + role.name + "<br />"
      end
      
      return html_output
    
  end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS