Forum: Ruby on Rails find email by account using active record

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0aa1671cc7245b6008c4289ddee4cc7c?d=identicon&s=25 Bob Kendall (rkendall)
on 2014-02-19 20:21
I am trying to refactor an app that I have inherited that was not coded
using rails best practices. I am fairly new to rails and I am doing the
tutorials and book thing, but I also need to keep making process on the
project.
--------------------------------------------------------


I have a model that I have refactored:
---------------------------------------------------------
models/weboel23.rb
class WEBOEL23 < ActiveRecord::Base
establish_connection "as400_#{RAILS_ENV}"
  set_table_name "WEBOEL23"

  def email
     @weboe123 = WEBOEL23
      WEBOEL23.where(ACT223: act223).first.email
  end
end
-------------------------------------
and I am calling it from models/order.rb:
if approval1!=""

Mailer.deliver_order_distributor_approval_email(@weboel23.email,
"Coastal Pet Online Ordering<noreply@coastalpet.com>", "DIST
Confirmation-1 ", email_details)
end
------------------------------------
the error that it is returning is - NoMethodError (Undefined method
'email' for nil:NilClass): - does this mean that the email record is
returning blank?  I have tried setting @weboe123 = WEBOEL23 in my def
but it is still coming back undefined. Does anyone have any insights or
clues that I may be missing??
--------------------------------
I have also tried:

models/weboel23.rb
class WEBOEL23 < ActiveRecord::Base
establish_connection "as400_#{RAILS_ENV}"
  set_table_name "WEBOEL23"

    def WEBOEL23
    @weboe123 = WEBOEL23
   end

   def get_email
    @WEBOEL23.where(ACT223: act223).first.email
  end
end

models/order.rb
if approval1!=""
Mailer.deliver_order_distributor_approval_email(weboel.get_email,
"Coastal Pet Online Ordering<noreply@coastalpet.com>", "DIST
Confirmation-1 ", email_details)
end
-------------------------------------------
executing the code this way results in the error NameError (undefined
local variable or method 'weboel' )
-----------------------------------------
thanks in advance for any help
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2014-02-19 20:50
(Received via mailing list)
On Wed, Feb 19, 2014 at 11:21 AM, Bob Kendall <lists@ruby-forum.com>
wrote:
> am trying to refactor an app that I have inherited that was not coded
> using rails best practices. I am fairly new to rails and I am doing the
> tutorials and book thing, but I also need to keep making process on the
> project.

Uh, well. Not sure that's a good idea, but whatever. Does this app
have decent test coverage? If not (and I'd bet I know the answer)
then that might be a better place to spend time to start...

Anyway,

> I have a model that I have refactored:

>   def email
>      @weboe123 = WEBOEL23

The line above makes zero sense.

>       WEBOEL23.where(ACT223: act223).first.email

> the error that it is returning is - NoMethodError (Undefined method
> 'email' for nil:NilClass): - does this mean that the email record is
> returning blank?

It means no records were found, so there's no 'first' to examine.

You might want to learn about using the console (irb, or better yet
pry) and trying out that line to see what it does, because I'd also bet
it's not what you're expecting  :-)

Good luck,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
This topic is locked and can not be replied to.