0

Tengo un método definido en mi modelo Affiliate de la siguiente manera:

def is_a_valid_email?
   (email =~ /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/)
end

Al ejecutarlo en mi rails console me tira el siguiente error: (aclaro que en mi variable a tengo guardado los datos de un afiliado especifico que yo busqué con un find_by_id):

2.6.3 :068 > a.email.is_a_valid_email?
Traceback (most recent call last):
    2: from (irb):68
    1: from (irb):68:in `rescue in irb_binding'
NoMethodError (undefined method `is_a_valid_email?' for "fr an. cof .. er7@gmail.com":String)

Codigo modelo affiliate:

# == Schema Information
#
# Table name: affiliates
#
#  id                :bigint           not null, primary key
#  code              :string           not null
#  start_at          :date             not null
#  active            :boolean          default(TRUE), not null
#  expiration_at     :date
#  person_id         :bigint           not null
#  id_persona        :integer
#  confirmed_at      :datetime
#  confirmation_code :string
#  user_id           :bigint
#  affiliate_type_id :bigint           not null
#  card_number       :string
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  email             :string
#
# Indexes
#
#  index_affiliates_on_affiliate_type_id  (affiliate_type_id)
#  index_affiliates_on_code               (code) UNIQUE
#  index_affiliates_on_person_id          (person_id)
#  index_affiliates_on_user_id            (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (affiliate_type_id => affiliate_types.id)
#  fk_rails_...  (person_id => people.id)
#  fk_rails_...  (user_id => users.id)
#

class Affiliate < ApplicationRecord

  # == Associations
  belongs_to :person, inverse_of: :affiliate
  belongs_to :user, optional: true

  belongs_to :category, class_name: 'AffiliateType', foreign_key: :affiliate_type_id

  has_many :affiliate_plans
  has_many :plans, through: :affiliate_plans
  has_many :authorisations
  has_many :relationships, foreign_key: :parent_id
  has_many :children, through: :relationships, source: :child

  has_one :inverse_relationship, foreign_key: :child_id, class_name: 'Relationship'
  has_one :parent, through: :inverse_relationship

  # == Validations
  validates :code, presence: true, uniqueness: true, null: false, length: {minimum:3}
  validates :person, presence: true
  validates :person_id, uniqueness: true, null: false
  validates :category, presence: true
  validates :confirmation_code, length: { is: 3 }, presence: true
  #validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  #validates :card_number, length: { is: 3 }, if: 'card_number.present?'

  # == Nested attributes
  accepts_nested_attributes_for :person, :reject_if => :all_blank

  # => scopes
  scope :actives, -> { where( active: true ) }
  scope :inactives, -> { where("active = ? or active is null", false) }
  scope :confirmed, -> { where("confirmed_at is not null") }
  scope :unconfirmed, -> { where("confirmed_at is null") }

  # == Constants
  RELATIONSHIPS = [
    'NO ESPECIFICADO',
    'ESPOSO/A',
    'HIJO/A',
    'PADRE',
    'MADRE',
    'NIETO/A',
    'SOBRINO/A',
    'HIJO/A DE CONYUGE'
  ]

  # == Class Methods
  def self.without_relationship
    Affiliate.where("affiliates.id not in
                       (SELECT parent_id FROM relationships
                                UNION
                        SELECT child_id FROM relationships)")
  end

  # == Instance Methods
  def is_a_valid_email?(email)
    (email =~ /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/)
    #email =~ Devise.email_regexp
  end

  def code_fullname
    "#{code} - #{person.try(:fullname)}"
  end

  def fullname
    person.try(:fullname)
  end

  def practice_used_odont_month
    #cantidad de practicas odontologicas consumidas en el mes 
     sum_dentistry(Date.new(Time.now.year,Time.now.month,1))
   end 

   def practices_used(practice, from=1.year.ago)
     sum_practices(practice.id, from)
   end

  def confirmed?
    confirmed_at
  end

  def actived?
    active
  end

  def confirm!(code)
    self.update_column(:confirmed_at, Time.now) if code.try(:to_s) == self.confirmation_code
  end

  def email_affiliate
    if category.code[0] == 'T' then
      email
    else
      parent.email unless parent.blank?  
    end 
  end

  private

  def sum_practices(practice_id, from)
    self.authorisations.consumed.joins(:items).where("authorisations.created_at > (?)", from).where("authorisation_practices.practice_id = ?", practice_id).sum(:quantity)
  end

  def sum_dentistry(from)
    pract = Practice.no_count_odont.pluck("id")
    self.authorisations.consumed.joins(:items).where("authorisations.created_at > (?) and authorisations.affiliate_id = ?", from, id).where("authorisation_practices.practice_id IN (?)", pract).sum(:quantity) 
  end  
end
1
  • el modelo esta instanciado ?
    – mariovzc
    el 24 jun. 2020 a las 14:10

1 respuesta 1

0

El método se debe llamar en la instancia de Affiliate, pero lo estás llamando en el atributo email (que es un String); así debería quedar:

a.is_a_valid_email?
7
  • Si, lo he probado de esa manera y me tira el siguiente error: NoMethodError (undefined method `is_a_valid_email?' for #<Affiliate:0x00007fd94fbc65f8>) el 4 jun. 2020 a las 15:01
  • @francofernandez ¿Podrías mostrar el código del modelo Affiliate?
    – Gerry
    el 4 jun. 2020 a las 15:35
  • Si, alli te lo adjunte a la pregunta. el 4 jun. 2020 a las 15:41
  • @francofernandez Se muestra el código del método pero no del modelo completo, ¿podrías incluir el código completo del modelo?
    – Gerry
    el 4 jun. 2020 a las 15:42
  • Ahora si, disculpa el 4 jun. 2020 a las 15:43

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.