title: Ruby on Rails: Remove blank entries in table
body: <p>I have an app where the user can create a new project and search for previous projects. On the "create new project" page, the user has the option to enter new info for each field, or select from dropdown menus, if the data is already in the database. As for the search function on the search page. The user can search on many different fields, multiple ways.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Following a suggestion below, I have gone ahead a built a table that holds the many technologies per project.</p>
  
  <p><strong>Search action</strong></p>
  
  <pre><code>def search
  
  @search = params[:client], params[:industry], params[:role], params[:tech_id], params[:business_div], params[:project_owner], params[:status], params[:start_date_dd], params[:start_date_A], params[:start_date_B],  params[:keywords]
  
  @project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)
  
  @search_performed = !@search.reject! { |c| c.blank? }.empty? 
  
     @project = Project.new(params[:project])
  
  
  @all_technols = Technol.all
  
  @technol = Technol.new(params[:tech])
  
  params[:technols][:id].each do |technol|
  
  if !technol.empty?
  
  @project_technol = @project.projecttechnols.build(:technol_id =&gt; technol)
  
  end
  end
  
  respond_to do |format|
        format.html # search.html.erb
        format.json { render :json =&gt; @project }
      end
  
  end
  </code></pre>
  
  <p><strong>New action:</strong></p>
  
  <pre><code>def new
      @project = Project.new
  
  @all_technols = Technol.all
  
  @project_technol = @project.projecttechnols.build
  
  
  @project_technol = Projecttechnol.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @project }
      end
  </code></pre>
  
  <p>end</p>
  
  <p><strong>Create action</strong></p>
  
  <pre><code>def create
      @all_technols = Technol.all
      @project = Project.new(params[:project])
  
  @technol = Technol.new(params[:tech])
  
  params[:technols][:id].each do |technol|
  
  if !technol.empty?
  
  
  
  @project_technol = @project.projecttechnols.build(:technol_id =&gt; technol)
  
  end
  end
      @project.client = params[:new_client] unless params[:new_client].blank?
      @project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?
      @project.tech = params[:new_tech] unless params[:new_tech].blank?
      @project.role = params[:new_role] unless params[:new_role].blank?
      @project.industry = params[:new_industry] unless params[:new_industry].blank?
      @project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
  
      respond_to do |format|
        if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully created.' }
          format.json { render json: @project, status: :created, location: @project }
        else
          format.html { render action: "new" }
          format.json { render json: @project.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p><strong>Tech part of the new view:</strong></p>
  
  <pre><code>&lt;%= stylesheet_link_tag "new" %&gt;
  
  
  &lt;h1&gt;Create New Project&lt;/h1&gt;
  
  
  
  
  
  &lt;div class="tech"&gt;
  &lt;%#= label_tag :new_tech, "Technologies Used :" %&gt;&lt;br/&gt;
  
  
  &lt;%= text_field_tag :new_tech, nil, :maxlength =&gt; 30 %&gt;
  
  Or
  &lt;%#= f.select( :tech, Project.all.map {|p| [p.tech]}.uniq, { :prompt =&gt; "Select a previous Technology"}, { :multiple =&gt; true, :size =&gt; 5 }) %&gt;
  &lt;!/div&gt;  
  
  &lt;% common_techs = [['Mainframe'],['UNIX'],['Windows Servers'],['Networking'],['CISCO'], ['Win7'], ['Telephony'], ['Web services'], ['Website'], ['Cloud'], ['Virtualisation'], ['Data Centre']] %&gt;
   &lt;% db_techs = Technol.all.map {|p| [p.tech]}.uniq %&gt;
  
  &lt;% all_tech = common_techs + db_techs %&gt;
  
  
  &lt;%= fields_for(@project_technol) do |ab| %&gt;
  
  
  
  
  &lt;%= ab.label "Select previous technologies" %&gt;&lt;br/&gt;
  
  &lt;%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple =&gt; true} ) %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
      &lt;%= stylesheet_link_tag "new" %&gt;
  
  
  &lt;h1&gt;Create New Project&lt;/h1&gt;
  
  
  
  
  
  &lt;div class="tech"&gt;
  &lt;%#= label_tag :new_tech, "Technologies Used :" %&gt;&lt;br/&gt;
  
  
  &lt;%= text_field_tag :new_tech, nil, :maxlength =&gt; 30 %&gt;
  
  Or
  &lt;%#= f.select( :tech, Project.all.map {|p| [p.tech]}.uniq, { :prompt =&gt; "Select a previous Technology"}, { :multiple =&gt; true, :size =&gt; 5 }) %&gt;
  &lt;!/div&gt;  
  
  &lt;% common_techs = [['Mainframe'],['UNIX'],['Windows Servers'],['Networking'],['CISCO'], ['Win7'], ['Telephony'], ['Web services'], ['Website'], ['Cloud'], ['Virtualisation'], ['Data Centre']] %&gt;
   &lt;% db_techs = Technol.all.map {|p| [p.tech]}.uniq %&gt;
  
  &lt;% all_tech = common_techs + db_techs %&gt;
  
  
  &lt;%= fields_for(@project_technol) do |ab| %&gt;
  
  
  
  
  &lt;%= ab.label "Select previous technologies" %&gt;&lt;br/&gt;
  
  &lt;%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple =&gt; true} ) %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  
  
  
  
  &lt;/div&gt; &lt;%#= small div %&gt;
  
  
  
  
  
  &lt;% end %&gt;
  
  
  
  &lt;div class="back_button2"&gt;
  &lt;%= button_to "Back", projects_path , :class =&gt; "button", :method =&gt; "get" %&gt;
  &lt;/div&gt;
  
  
  
  &lt;/div&gt; &lt;%#= small div %&gt;
  
  
  
  
  
  &lt;% end %&gt;
  
  
  
  &lt;div class="back_button2"&gt;
  &lt;%= button_to "Back", projects_path , :class =&gt; "button", :method =&gt; "get" %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Search Page:</strong></p>
  
  <pre><code>&lt;html&gt;
  
  &lt;%= stylesheet_link_tag "search" %&gt;
  &lt;body&gt;
  &lt;div id ="title1"&gt;Exception Database Search&lt;/div&gt;
  &lt;div id = "search_op"&gt;
  
  &lt;%= form_tag search_path, method: :get do %&gt;
  
  
  &lt;div class="client"&gt;
  Client : 
  &lt;%= select(@projects, :client, Project.order("client").map{|p| [p.client]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:client]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  &lt;div class="industry"&gt;
  Industry :
  &lt;%= select(@projects, :industry, Project.order("industry").map {|p| [p.industry]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:industry]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  &lt;div class="role"&gt;
  Role :
  &lt;%= select(@projects, :role, Project.order("role").map {|p| [p.role]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:role]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  &lt;div class="tech"&gt;
  
  &lt;% common_techs = [['Mainframe'],['UNIX'],['Windows Servers'],['Networking'],['CISCO'], ['Win7'], ['Telephony'], ['Web services'], ['Website'], ['Cloud'], ['Virtualisation'], ['Data Centre']] %&gt;
   &lt;% db_techs = Project.order("tech").map {|p| [p.tech]}.uniq %&gt;
  
  &lt;% all_tech = Project.order("tech").map {|p| [p.tech]} + Project.order("tech").map {|p| [p.tech2]} + Project.order("tech").map {|p| [p.tech3]} + Project.order("tech").map {|p| [p.tech4]} + Project.order("tech").map {|p| [p.tech5]} %&gt;
  
  
  
  
  
  &lt;%= fields_for(@project_technol) do |ab| %&gt;
  
  
  
  &lt;%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple =&gt; true } ) %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  
  &lt;div class="business_div"&gt;
  Business Division :
  
  &lt;%= select(@projects, :business_div, Project.order("business_div").map {|p| [p.business_div]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:business_div]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  
  &lt;div class="project_owner"&gt;  
  Project Owner :
  &lt;%= select(@projects, :project_owner, Project.order("project_owner").map {|p| [p.project_owner]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:project_owner]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  
  &lt;div class="date1"&gt;
  Select start dates from :
  &lt;%= select_tag "start_date_dd", options_for_select({
      "Select a period" =&gt; "",
        "3 days ago"   =&gt; DateTime.now.to_date - 3.days,   # =    259_200 sec.
        "1 week ago"   =&gt; DateTime.now.to_date - 1.week,   # =    604_800 sec.
        "1 month ago"  =&gt; DateTime.now.to_date - 1.month,  # =  2_592_000 sec.
        "6 months ago" =&gt; DateTime.now.to_date - 6.months, # = 15_552_000 sec.
        "1 year ago"   =&gt; DateTime.now.to_date - 1.year,   # = 31_557_600 sec.
      }, :selected=&gt;params[:start_date_dd] )%&gt;
  
  &lt;/div&gt;
  
  &lt;%#= until now &lt;%= l DateTime.now.to_date %&gt;&lt;%#=,%&gt;   
  &lt;h4&gt; OR&lt;/h4&gt; 
  
  &lt;div class="date2"&gt;
  exact start dates
  
  &lt;%=  text_field_tag :start_date_A, params[:start_date_A], :style =&gt; 'width: 80px;' %&gt;
  
  
  -
  
  &lt;%=  text_field_tag :start_date_B, params[:start_date_B], :style =&gt; 'width: 80px;' %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  
  
  
  
  
  &lt;div class="status"&gt;
  Status :
  
  &lt;%= select(@projects, :status, Project.order("status").map {|p| [p.status]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:status]) %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  
  
  &lt;div class="keywords"&gt;
  Keywords :
  
  &lt;%= text_field_tag :keywords, params[:keywords] %&gt;&lt;/br&gt;
  &lt;/div&gt;
  
  
  
  
  &lt;div class="results"&gt;
  Results per page: &lt;%= select_tag :per_page, options_for_select([10,20,50], :selected=&gt;params[:per_page]),  { :onchange =&gt; "this.form.submit();"}   %&gt;&lt;/br&gt;
  
  
  &lt;div class="buttons"&gt;
  &lt;div id="search_button"&gt;
  &lt;%= submit_tag "Search", name: nil, :class =&gt; "button" %&gt;
  &lt;/div&gt;
  
  &lt;% end %&gt;
  
  &lt;div class="home_button"&gt;
  &lt;%= button_to "Home", projects_path, :class =&gt; "button", :method =&gt; "get"  %&gt;
  &lt;/div&gt;
  
  
  
  &lt;div id="reset_button"&gt;
  &lt;%= button_to "Reset Search", search_path, :class =&gt; "button", :method =&gt; "get" %&gt;
  &lt;/div&gt;
  
  
  &lt;/div&gt;
  &lt;/div&gt; &lt;%#search_op div%&gt;
  
  
  
  
  
  
  
  &lt;div class ="menu"&gt;&lt;/div&gt;
  
  
  
  &lt;div id ="section3"&gt;
  
  &lt;% if @project_search.total_entries &gt; 0 %&gt;
  &lt;% if @search_performed %&gt;
  
  
  
  
  
  &lt;% if @search_performed %&gt;
  &lt;%= hidden_field_tag :direction, params[:direction] %&gt;
  &lt;%= hidden_field_tag :sort, params[:sort] %&gt;
  &lt;%= hidden_field_tag :per_page, params[:per_page] %&gt;
  &lt;%= hidden_field_tag :page, params[:page] %&gt;
  
  
  &lt;%= will_paginate (@project_search), :class =&gt; 'will' %&gt;
  
  &lt;% end %&gt;
  
  
  
  &lt;/body&gt;
  
  
  &lt;/html&gt; 
  </code></pre>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>class Projecttechnol &lt; ActiveRecord::Base
    attr_accessible :project_id, :technol_id
  
  belongs_to :technol
  belongs_to :project
  end
  
  class Technol &lt; ActiveRecord::Base
    attr_accessible :tech
  
  has_many :projecttechnols
  has_many :projects, :through =&gt; :projecttechnols
  end
  
  class Project &lt; ActiveRecord::Base
    attr_accessible :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date,  :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech_id
  
  validates_presence_of :business_div, :client, :customer_benifits, :end_date,  :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary#, :tech
  
  
  validates_format_of :industry, :with =&gt; /\A[a-zA-Z]+\z/, :message =&gt; "field should only have letters"
  validates_format_of :business_div, :with =&gt; /\A[a-zA-Z]+\z/, :message =&gt; "field should only have letters"
  validates_format_of :client, :with =&gt; /\A[a-zA-Z]+\z/, :message =&gt; "field should only have letters"
  validates_format_of :project_owner, :with =&gt; /\A[a-zA-Z]+\z/, :message =&gt; "field should only have letters"
  validates_format_of :role, :with =&gt; /\A[a-zA-Z]+\z/, :message =&gt; "field should only have letters"
  
  has_many :projecttechnols
  has_many :technols, :through =&gt; :projecttechnols
  
  
  def self.like(text); "%#{text}%"; end
  
    def self.search(search_client, search_industry, search_role, search_tech_id, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date_dd, search_start_date_A, search_start_date_B,  search_keywords)
      # start with a scoped query, to apply more scopes on it afterwards
      _projects = Project.scoped 
      # then, for each of the parameters, apply the scope only if present
      if search_client.present?
        _projects = _projects.where ['client LIKE ?', like(search_client)] 
      end
      if search_industry.present?
        _projects = _projects.where ['industry LIKE ?', like(search_industry)]
      end
      if search_role.present?
        _projects = _projects.where ['role LIKE ?', like(search_role)]
      end
  
  
      if search_tech_id.present?
    _projects = _projects.joins(:technols).where("tech.id" =&gt; search_tech_id)
  end
  
      if search_business_div.present?
        _projects = _projects.where ['business_div LIKE ?', like(search_business_div)]
      end
      if search_project_owner.present?
        _projects = _projects.where ['project_owner LIKE ?', like(search_project_owner)]
      end
  
       if search_status.present?
        _projects = _projects.where ['status LIKE ?', like(search_status)]
      end
  
  
  
  todays_date = DateTime.now.to_date
  
  if !search_start_date_A.blank? or !search_start_date_B.blank?
      search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
      search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
      todays_date = nil
      search_start_date_dd = nil
  
      end
  
  if search_start_date_dd.blank?
      todays_date = nil
  end
  
  
  if search_start_date_A.present? or search_start_date_B.present?
  
        _projects = _projects.where [' DATE(start_date) BETWEEN ? AND ?', search_start_date_A, search_start_date_B]
      end
  
  
                  if search_start_date_dd.present?
        _projects = _projects.where ['DATE(start_date) BETWEEN ? AND ?', search_start_date_dd, todays_date]
      end
  
  
  
  
      if search_keywords.present?
        _projects = _projects.where ['keywords LIKE ?', like(search_keywords)]
      end
      # now you have applied only the present scopes. return the result, and watch 
      # the query as it executes.
      _projects
    end
  
  
  def self.paginated_for_index(projects_per_page, current_page)
      paginate(:per_page =&gt; projects_per_page, :page =&gt; current_page)
    end
  
  end
  </code></pre>
  
  <p>I can't seem to get the new technologys on the "new project page" to save in the Technols table. I also can't get the search to work properly. Searching for anything in the technology field returns nothing.</p>