class User < AR has_many_belongs_to :roles end
in User model Edit view
<%Role.find(:all,:select=>"id,name").each do |role|%>
<%=check_box_tag
name="user[role_ids][]",role.id,@user.role_ids.include?(role.id)%><%=role.name%>
<%end%>
in has_many :through (or) has_and_belongs_to_many
association
when editing user details if you unselect all role name(means no
role assigned to that user like public user ) options then it won't
update in the intermediate (or) join_table
because params[:user][:role_ids] is nil
to avoid that you can add one more hidden with zero length
string value like this
<%Role.find(:all,:select=>"id,name").each do |role|%>
<%=check_box_tag
name="user[role_ids][]",role.id,@user.role_ids.include?(role.id)%><%=role.name%>
<%end%>