Question

I have a table users, which has one common_app. I'd like to make the text in a user's common_app.cover_letter, searchable.

When I try to do this, I get the error --

rake sunspot:reindex
undefined method `map' for nil:NilClass
/Users/stepan/Desktop/Previous Desktop/Sites/atlas/app/models/user.rb:45:in `block (2 levels) in <class:User>'

This is how my User Model looks like --

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  name                   :string(255)
#  email                  :string(255)
#  created_at             :datetime
#  updated_at             :datetime
#  password_digest        :string(255)
#  remember_token         :string(255)
#  admin                  :boolean          default(FALSE)
#  password_reset_token   :string(255)
#  password_reset_sent_at :datetime
#  heat_id                :integer
#

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_create :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, 
            format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }

  has_secure_password validations: false

  validates :password, length: { minimum: 6 }, allow_blank: true
  validates :password, presence: true, on: :create

  belongs_to :heat

  has_one :common_app, dependent: :destroy

  has_one :video, dependent: :destroy

  has_many :applications, dependent: :destroy
  has_many :jobs, :through => :applications

  searchable do 
    text :name, :email
    text :common_app do
      common_app.map { |common_app| common_app.cover_letter }
    end
  end

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column) # this is similar to create_remember_token, but instead it's generalized, so it can work on any column
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end

  def beginner? 
    false if self.common_app || self.video || self.jobs.any?
  end

  def first_name
    self.name.split(' ')[0].capitalize
  end

  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end

end

I'm guessing this is happening because some users don't have a common_app, or I'm referencing the has_one association incorrectly.

Help much appreciated!

Was it helpful?

Solution

I think this should work.

  text :common_app do
    common_app.cover_letter
  end

It's a has_one relation which means you can't be mapping over it. You would map over if it were a has_many :common_apps relation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top