I would like to create an object equal to a query in the database

Asked

Viewed 48 times

1

I would like to create an object with the fields equal to the query of Activerecord, however, with the fields pre-filled or blank, so that they are called in the same way in the other file...

To illustrate better, the code snippet that I

      def organization_name_for_emails(organization = @organization)
        @organization_name_for_emails ||= organization.emails.first
        if @organization_name_for_emails == "" || @organization_name_for_emails.blank?
          #CRIAR O OBJETO
          return @organization_name_for_emails = "RETORNO QUE EU 
QUISER DAR DA TABELA, NESTE CASO GOSTARIA DE DAR UM VALOR 
chamado "Nome" para o campo "name" da tabela que irá receber no outro arquivo."
        else
          return @organization_name_for_emails
        end
      end

In this case it would have to work on this code snippet,

  def confirm_email(event, participant)
    @event = event
    @organization = event.owner_organization
    @participant = participant
    **@organization_name_for_emails = organization_name_for_emails(@organization)
    subject = I18n.t("views.mailer.event_registration.confirm_email.subject", :name_subject => @organization_name_for_emails.name)**
    destination = AppConfig[:send_emails] ? @participant.email : AppConfig["default_email"]
    mail(:to => destination, :subject => subject)
  end

Has anyone ever gone through this, has a better solution or some hint?

Thank you!

  • I couldn’t understand very well what you want. =\

  • So I solved the problem differently...

1 answer

1


Personal thanks for those who were willing to help, I arranged the problem in another way, I used an array and I took the answer by array, it worked out all right.

  def options_to_send_email
    @email_default ||= self.emails.first
    if @email_default.present?
      [@email_default.email, @email_default.name.present? ? @email_default.name : 'APP']
    else
      [AppConfig[:from_email], 'APP']
    end
  end

And to get the information I used the following option.

email, name  = @organization.options_to_send_email

So I didn’t have to create an object for the answer I needed...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.