I am new to the language and trying to add a couple of simple tests on user input, followed by a DB query, then using the returned values (where the problem lies) I first run a find_by_sql similar to this @my_names = modelname.find_by_sql I then check to see if NO returns via if @my_names.size == 0 ================= My question is, how do I access the values that are now in the array. I seem to be able to set session variables by way of session[:whatever] = @my_names[0].column_name etc.. but am unable to perform a if construct that looks like if @my_names[0].column_name == "TEST VALUE" I keep getting an undefined method error when I try to run the code. Thanks very much in advance for any assistance with this. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 7 November 2012 20:07, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am new to the language and trying to add a couple of simple tests on > user input, followed by a DB query, then using the returned values > (where the problem lies) > > I first run a find_by_sql similar to this > > @my_names = modelname.find_by_sqlI know this is not part of your question, but as a newcommer to rails I think it is very unlikely that you should be using find_by_sql. There are not many situations that this is necessary, there is almost certainly another way. You might like to ask about this in another thread to see if there is a better way.> > I then check to see if NO returns via > > if @my_names.size == 0 > > =================> > My question is, how do I access the values that are now in the array. I > seem to be able to set session variables by way of > > session[:whatever] = @my_names[0].column_name > etc.. > > but am unable to perform a if construct that looks like > > if @my_names[0].column_name == "TEST VALUE" > > I keep getting an undefined method error when I try to run the code. > Thanks very much in advance for any assistance with this.Show us the actual code and the full error message please. First look carefully at the error and try to interpret it, often the clue is in the message but may not be initially obvious to a newcommer. Finally, if you have not already done so, I recommend working through a good tutorial on rails. railstutorial.org is good and is free to use online. Make sure that you use a tutorial for rails 3 and that you use exactly the right version of rails. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi Colin, This is old code (Rails v 1.1.6 - Ruby 1.8.5) that I am working with, and don''t want to migrate it too to current version because it is not long term. The code is almost exactly as cited above, just have the column names removed. The error code tells me that NoMethodError (undefined method `province'' for nil:NilClass): /path/file.rb:302:in `set_no'' So I look on that line, in the set_no definition, which is where I was trying to call this line if @my_names[0].province == "XX" province is one of the column names that is returned from the find_by_sql call. Does this make any more sense now ? I have been through many attempts to resolve this, and can work with parameters, etc but with DB returns it seems I am missing something. Colin Law wrote in post #1083450:> On 7 November 2012 20:07, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I am new to the language and trying to add a couple of simple tests on >> user input, followed by a DB query, then using the returned values >> (where the problem lies) >> >> I first run a find_by_sql similar to this >> >> @my_names = modelname.find_by_sql > > I know this is not part of your question, but as a newcommer to rails > I think it is very unlikely that you should be using find_by_sql. > There are not many situations that this is necessary, there is almost > certainly another way. You might like to ask about this in another > thread to see if there is a better way. > >> session[:whatever] = @my_names[0].column_name >> etc.. >> >> but am unable to perform a if construct that looks like >> >> if @my_names[0].column_name == "TEST VALUE" >> >> I keep getting an undefined method error when I try to run the code. >> Thanks very much in advance for any assistance with this. > > Show us the actual code and the full error message please. First look > carefully at the error and try to interpret it, often the clue is in > the message but may not be initially obvious to a newcommer. > > Finally, if you have not already done so, I recommend working through > a good tutorial on rails. railstutorial.org is good and is free to > use online. Make sure that you use a tutorial for rails 3 and that > you use exactly the right version of rails. > > Colin-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 7 November 2012 22:06, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Colin, > > This is old code (Rails v 1.1.6 - Ruby 1.8.5) that I am working with, > and don''t want to migrate it too to current version because it is not > long term. > > The code is almost exactly as cited above, just have the column names > removed. > The error code tells me that > > NoMethodError (undefined method `province'' for nil:NilClass): > /path/file.rb:302:in `set_no'' > > So I look on that line, in the set_no definition, which is where I was > trying to call this line > > if @my_names[0].province == "XX"As I said, the clue is in the error message. Undefined method province for nil:NilClass. This is saying that you have tried to call the method province on a nil object, which means that @my_names[0] is nil. Colin> > province is one of the column names that is returned from the > find_by_sql call. > > Does this make any more sense now ? I have been through many attempts to > resolve this, and can work with parameters, etc but with DB returns it > seems I am missing something. > > Colin Law wrote in post #1083450: >> On 7 November 2012 20:07, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> I am new to the language and trying to add a couple of simple tests on >>> user input, followed by a DB query, then using the returned values >>> (where the problem lies) >>> >>> I first run a find_by_sql similar to this >>> >>> @my_names = modelname.find_by_sql >> >> I know this is not part of your question, but as a newcommer to rails >> I think it is very unlikely that you should be using find_by_sql. >> There are not many situations that this is necessary, there is almost >> certainly another way. You might like to ask about this in another >> thread to see if there is a better way. >> >>> session[:whatever] = @my_names[0].column_name >>> etc.. >>> >>> but am unable to perform a if construct that looks like >>> >>> if @my_names[0].column_name == "TEST VALUE" >>> >>> I keep getting an undefined method error when I try to run the code. >>> Thanks very much in advance for any assistance with this. >> >> Show us the actual code and the full error message please. First look >> carefully at the error and try to interpret it, often the clue is in >> the message but may not be initially obvious to a newcommer. >> >> Finally, if you have not already done so, I recommend working through >> a good tutorial on rails. railstutorial.org is good and is free to >> use online. Make sure that you use a tutorial for rails 3 and that >> you use exactly the right version of rails. >> >> Colin > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi Colin, I don''t think it is, in fact I know it isn''t empty because the logic is inside a test for the return size of 0. if @my_names.size == 0 render :update do |page| // Bunch of updating code to the displayed message to indicate no match found. page[:name].visualEffect(''highlight'') end else // This is where I try to test the code of if @my_names[0].province == "XX" // Then want to do some other things. else // Set sessions variables. session[:var_a] = @my_names session[:var_b] = @my_names[0].first_name session[:var_c] = @my_names[0].last_name session[:var_d] = @my_names[0].line_1 session[:var_d] = (@my_names[0].line_2 || '''') session[:var_e] = (@my_names[0].province || '''') ---------- Any thoughts ? It doesn''t seem to matter if I test AFTER the initial test for 0 or before (knowing that I am going to get a match because of predefined values that exist in the database.) I always get the same error. Thanks again in advance. Colin Law wrote in post #1083465:> On 7 November 2012 22:06, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> NoMethodError (undefined method `province'' for nil:NilClass): >> /path/file.rb:302:in `set_no'' >> >> So I look on that line, in the set_no definition, which is where I was >> trying to call this line >> >> if @my_names[0].province == "XX" > > As I said, the clue is in the error message. Undefined method > province for nil:NilClass. This is saying that you have tried to call > the method province on a nil object, which means that @my_names[0] is > nil. > > Colin-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder
2012-Nov-07 22:59 UTC
Re: Re: Re: find_by_sql - ActiveRecord - Help please.
On Wed, Nov 7, 2012 at 2:49 PM, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I don''t think it is, in fact I know it isn''t empty because the logic is > inside a test for the return size of 0.1.9.3 (main):0 > @stuff = [] => [] 1.9.3 (main):0 > @stuff << nil => [ [0] nil ] 1.9.3 (main):0 > @stuff[0] => nil 1.9.3 (main):0 > @stuff.size => 1 1.9.3 (main):0 > You might want to try a different approach to validating the content of your array :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Another example: data = Array.new(10, nil) p data --output:-- [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] p data.size --output:-- 10 puts data[0].province --output:-- 1.rb:4:in `<main>'': undefined method `province'' for nil:NilClass (NoMethodError) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 7 November 2012 22:49, Jeff Lockyer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Colin, > > I don''t think it is, in fact I know it isn''t empty because the logic is > inside a test for the return size of 0. > > if @my_names.size == 0 > > render :update do |page| > // Bunch of updating code to the displayed message to > indicate no match found. > page[:name].visualEffect(''highlight'') > endThis end would appear to close the if statement.> > elseI am surprised that does not cause a syntax error. Also as others have pointed out the first element of the array could be nil. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
The "end" closes the "render :update do |page| " -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vmZL0qwpz60J. For more options, visit https://groups.google.com/groups/opt_out.
On 8 November 2012 12:56, Timster <timshaffer00-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The "end" closes the "render :update do |page| "Good job someone is paying proper attention. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.