# Newb question...get info from the db

**URL:** <https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438>\
**Category:** Rails\
**Created:** [March 10, 2009, 12:04am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438 "2009-03-10T00:04:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:04am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/1 "2009-03-10T00:04:06Z")

</div>

I am using a mysql db and I want to pull some info from the db. I think  
my problem is that I am trying to get the data from an object that  
doesn’t exist in my MemberController. My code:

```auto
class MemberController < ApplicationController
  def index
    @login = User.find_by_login(params[:l])
    flash[:notice] = @login + ", you've logged in successfully"
  end
end
```

the table in the db is the User table and it has a column of login.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:49am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/2 "2009-03-10T00:49:00Z")

</div>

In that case, try:

class MemberController \< ApplicationController  
def index  
@user= User.find\_by\_login(params[:l])  
flash[:notice] = @user.login + “, you’ve logged in successfully”  
end  
end

On Mar 9, 7:24 pm, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:17am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/3 "2009-03-10T00:17:58Z")

</div>

Hi Chris,

On Tue, 2009-03-10 at 00:04 +0100, Chris G. wrote:

> I am using a mysql db and I want to pull some info from the db. I think  
> my problem is

You’ll find it easier to get help here if you’ll post the error message  
that demonstrates your problem, along with the code that the error  
message tells you is causing the problem.

> that I am trying to get the data from an object that  
> doesn’t exist in my MemberController. My code:
> 
> ```auto
> class MemberController < ApplicationController
> > def index
> > @login = User.find_by_login(params[:l])
> > flash[:notice] = @login + ", you've logged in successfully"
> > end
> > end
> ```

However, in this case you may well have told us the probable cause of  
the problem.

> the table in the db is the User table and it has a column of login.

Rails’ default is that the table name is a plural version of the model  
name. That’s because the Model typically represents a single record  
from a table. So the table named Users holds lots of records that are  
access via the User model.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:54am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/4 "2009-03-10T00:54:16Z")

</div>

On Tue, 2009-03-10 at 00:24 +0100, Chris G. wrote:

> {“l”=\>“ballhogjoni”}

What does the index method in member\_controller.rb look like?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:24am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/5 "2009-03-10T00:24:09Z")

</div>

> Rails’ default is that the table name is a plural version of the model  
> name. That’s because the Model typically represents a single record  
> from a table. So the table named Users holds lots of records that are  
> access via the User model.

Im sorry the table name is users…here is the error message:  
NoMethodError in MemberController#index

undefined method `+’ for #User:0x44d3ca4

RAILS\_ROOT: C:/Ruby/MyProjects/ipod-give-away.com  
Application Trace | Framework Trace | Full Trace

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active\_record/attribute\_methods.rb:260:in  
`method_missing' app/controllers/member_controller.rb:4:in`index’

Request

Parameters:

{“l”=\>“ballhogjoni”}

Show session dump

Response

Headers:

{“cookie”=\>[],  
“Content-Type”=\>"",  
“Cache-Control”=\>“no-cache”}

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 1:05am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/6 "2009-03-10T01:05:59Z")

</div>

OK Great…

You were calling the + method, which concatenates two string objects, on  
a  
User object. In your original code, the @login variable did not hold a  
string object, but a user object (returned by the call to  
User.find\_by\_login( … ).

Ruby was rightfully complaining that the + method was not defined on the  
user object. @user.login returns a string, which does have the + method  
and  
therefore you can concatenate with the rest of your string (in the flash  
message).

On Mon, Mar 9, 2009 at 7:59 PM, Chris G. \<

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 1:11am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/7 "2009-03-10T01:11:12Z")

</div>

Harold A. GimÃ©nez Ch. wrote:

> OK Great…
> 
> You were calling the + method, which concatenates two string objects, on  
> a  
> User object. In your original code, the @login variable did not hold a  
> string object, but a user object (returned by the call to  
> User.find\_by\_login( … ).
> 
> Ruby was rightfully complaining that the + method was not defined on the  
> user object. @user.login returns a string, which does have the + method  
> and  
> therefore you can concatenate with the rest of your string (in the flash  
> message).
> 
> On Mon, Mar 9, 2009 at 7:59 PM, Chris G. \<

oooo ok in PHP (what im used to programming), I was assigning @login =  
User.find\_by\_login(params[:l]) which would, in php, tell php the @login  
is a string and not an object. I have to get used to everything being an  
object in ruby.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 10, 2009, 12:59am UTC](https://www.ruby-forum.com/t/newb-question-get-info-from-the-db/162438/8 "2009-03-10T00:59:36Z")

</div>

Harold wrote:

> In that case, try:
> 
> class MemberController \< ApplicationController  
> def index  
> @user= User.find\_by\_login(params[:l])  
> flash[:notice] = @user.login + “, you’ve logged in successfully”  
> end  
> end
> 
> On Mar 9, 7:24ï¿½pm, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Your changes worked…What was I doing wrong?
