ruby-on-rails-4autocompletesimple-formmodel-associationsvirtual-attribute

rails simple_form using virtual attributes


I am trying to incorporate Simple_forms into my app using a virtual attributes. I am following http://railscasts.com/episodes/102-auto-complete-association-revised to get autocomplete to work as well. Simple_form works when i do not use a virtual attribute but when I do use the virtual attribute I get the error "Association :head_coach_name not found".

My Team Model is:

class Team < ActiveRecord::Base
  attr_accessor :head_coach_name
  belongs_to :user
  validates :team_name, presence:   true
  belongs_to :head_coach, class_name: "User", :foreign_key => "head_coach_id"

  def head_coach_name
    user.try(:name)
  end

  def head_coach_name=(name)
    self.user = User.find_by_name(name) if name.present?
  end
end

My User model is:

class User < ActiveRecord::Base
  has_many :teams, :class_name => "::Team", dependent: :destroy
end

My View:

                <%= simple_form_for @team, html: {class: 'form-horizontal' }, url: teams_path, method: 'post' do |f| %>     
                <%= f.error_notification %>         
                <%= f.hidden_field :user_id %>                  
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-12">                         
                                <%= f.input :team_name, class: "form-control" %>        
                                <%= f.input :year, collection: Date.today.year-90..Date.today.year  %>  
                                <%= f.input :season, as: :select, collection: 
                                    ['Fall',
                                    'Winter',
                                    'Spring',
                                    'Summer'] %>                        
                                <%= f.input :season_type, as: :select, collection:
                                    ['Outdoor',
                                    'Indoor',
                                    'Tournament'] %>
                                <%= f.input :awards %>  
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-12">         
                                <%= f.input :club %>        
                                <%= f.input :division %>
                                <%= f.association :head_coach_name %>                       
                                <%= f.input :assistant_coach_id %>          
                                <%= f.input :assistant_coach_two_id %>
                            </div>                                              
                        </div>
                    </div>          
                <%= f.button :submit, label: "Add team", class: "btn btn-large btn-primary col-md-3 pull-right" %>
                <% end %>   

Everything works as long as i don't have the virtual association in there. I could put :head_coach and it would work with a drop down list but I want to use the autocomplete feature like in the railscast video. Also in rails console i can run these commands to show the virtual attribute works:

2.1.2 :003 > team.head_coach_name
  User Load (0.6ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 100 LIMIT 1
 => "Miss Daisha Shanahan"

Any ideas on how I can get the virtual attribute to work correctly with simple_forms?


Solution

  • You are referencing an association that doesn't exist with f.association :head_coach_name. Try this instead:

    f.association :head_coach, label_method: :head_coach_name
    

    You have a few other oddities in your code, including your head_coach_name definition, which should rely on head_coach instead of user. Same concern for the head_coach_name= method.

    Example. You have:

    def head_coach_name
      user.try(:name)
    end
    

    Seems like you should have:

    def head_coach_name
      head_coach.try(:name)
    end