Private method select called for classRails

I have the following code inside my controller:

array = Contact.select(:name).distinct

The idea is that this would create an array of all Contact models with a unique :name attribute. However, it's throwing this error:

NoMethodError (private method 'select' called for Contact:Class)

What's the misunderstanding here? For what it's worth, the method calling this line of code is not defined in the controller as private.

EDIT:

Here's the actual code:

Controller

class FluidsurveysController < ApplicationController

  def index
  end

  def import_contacts
    @survey_provider = FluidsurveysProviders::SurveyProvider.new()

    @current_month = Time.new.strftime("%B%Y%d")

    fs_contact_list_array = csv_to_array(params[:file].tempfile)
    @fs_contacts_array = []

    fs_contact_list_array.each do |hash|
      @fs_contacts_array << Contact.new(hash)
    end

   array = Contact.select(:name).distinct
   end
end

Model

class Contact
  include ActiveModel::Model
  attr_reader :client_id, :client_name, :branch_id, :branch, :short_name, :unit_id, :membership_id,
              :first_name, :last_name, :date_of_birth, :change_date_time, :membership_type,
              :home_phone, :email_address, :anniversary_years


  def initialize(fs_contact_hash = {})
    @client_id = fs_contact_hash.fetch('ClientID')
    @client_name = fs_contact_hash.fetch('ClientName')
    @branch_id = fs_contact_hash.fetch('branchID1')
    @branch = fs_contact_hash.fetch('branch')
    @name = fs_contact_hash.fetch('ShortName')
    @unit_id = fs_contact_hash.fetch('UnitID')
    @membership_id = fs_contact_hash.fetch('MemberID')
    @first_name = fs_contact_hash.fetch('FirstName')
    @last_name = fs_contact_hash.fetch('LastName')
    @date_of_birth = fs_contact_hash.fetch('DateOfBirth')
    @change_date_time = fs_contact_hash.fetch('ChangeDateTime')
    @membership_type = fs_contact_hash.fetch('MembershipType')
    @home_phone = fs_contact_hash.fetch('HomePhone')
    @email_address = fs_contact_hash.fetch('EMail1')
    @anniversary_years = fs_contact_hash.fetch('Years')
  end
end
2 Answers
  1. Based on your error message I am pretty sure your model is not an ActiveRecord Object.

    If you want to make use of ActiveRecord#select, define your model like this.

    class Contact < ActiveRecord::Base
    

    Also you need to define your attributes in a database instead of via attr_reader to access them through ActiveRecord. See http://guides.rubyonrails.org/getting_started.html#running-a-migration

    2013-11-13 16:10:19
  2. You seem to be using an older version of Rails, specifically version 2.3.whatever. There, the select method is indeed private on the ActiveRecord model classes (as it is inherited from the Kernel module which is part of every Ruby object and serves a wholly different purpose) and thus isn't intended to be used like it is done in Rails 3 and Rails 4.

    In Rails 2.3, you can achieve similar results using this syntax:

    Contact.all(:select => "DISTINCT name")
    

    This will return an array of Contacts which have only the name attribute set.

    2013-11-13 15:54:52
Related Articles
You Might Also Like