# Rails and Databases

**URL:** <https://www.ruby-forum.com/t/rails-and-databases/124638>\
**Category:** Rails\
**Created:** [December 31, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638 "2007-12-31T12:43:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [December 31, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/1 "2007-12-31T12:43:15Z")

</div>

Hello,

I am new to RoR and have several questions.

1. I have an existing DB and I want to develop an application that uses  
this  
existing DB. How do I import this DB into my application. What I mean  
is, I  
dont want to generate new models using rails, I want to use the existing  
DB.

2. My DB contains tables named abc\_profile, abc\_friends and  
abc\_register.  
One profile can have many friends and one profile can have one register.  
What I have done is, I used the generate model script to generate the 3  
models, and in the AbcProfile class, I wrote has\_many :abc\_friends and  
also  
wrote has\_one :abc\_register. In the AbcFriends class I wrote belongs\_to  
:abc\_profile, and in the AbcRegister class I also wrote belongs\_to  
:abc\_profile. Then I made a profile controller (generate controller  
Profile)  
and wrote in it @result = AbcProfile.find(:all).

In the app/views/profile/index.rhtml file, I wrote \<% @result.each do  
|res|  
%\>, but this line is giving an error. The error is

NoMethodError in Profile#index

Showing _profile/index.rhtml_ where line _#1_ raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line _#1_):

1: \<% @result.each do |res| %\>  
2:

  
3:   
4: 

| 

Please help me with this error, and also guide me if I have done  
anything  
wrong in the above mentioned code. Also tell me about the table names  
and  
about the underscore in the table names, I mean do I write has\_many  
:abc\_profile, or do I write has\_many :AbcProfile.

Any help will be highly appreciated

Please reply to the email address removed\_email\_address@domain.invalid

–  
Regards

Haris G.

 |

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [December 31, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/2 "2007-12-31T13:31:15Z")

</div>

Hello,

Am Montag, 31. Dezember 2007 schrieb Haris G.:

> Hello,
> 
> I am new to RoR and have several questions.
> 
> 1. I have an existing DB and I want to develop an application that uses  
> this existing DB. How do I import this DB into my application. What I mean  
> is, I dont want to generate new models using rails, I want to use the  
> existing DB.

If not set somewhere, models are more or less generated on the fly from  
your  
tables.

> 1. My DB contains tables named abc\_profile, abc\_friends and abc\_register.  
> One profile can have many friends and one profile can have one register.  
> What I have done is, I used the generate model script to generate the 3  
> models, and in the AbcProfile class, I wrote has\_many :abc\_friends and also  
> wrote has\_one :abc\_register. In the AbcFriends class I wrote belongs\_to

If not set elsehwere, table-names ought to be plural. Have you set this  
according to your needs?

> :abc\_profile, and in the AbcRegister class I also wrote belongs\_to  
> :abc\_profile. Then I made a profile controller (generate controller  
> : Profile)
> 
> and wrote in it @result = AbcProfile.find(:all).
> 
> In the app/views/profile/index.rhtml file, I wrote \<% @result.each do |res|  
> %\>, but this line is giving an error. The error is
> 
> NoMethodError in Profile#index

Seems your controller’s index-Method is not found - it’s hard to say  
what went  
wrong, if you don’t show your controller completely (pastie.caboo.se is  
your  
friend)

> 3:
> 
>   
> 4: 
> 
> @results is nil (in Java Terms null) 'cause it hasn’t been definied,  
> 'cause  
> your controller wasn’t executed.
> 
> Keep smiling  
> yanosz

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [December 31, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/3 "2007-12-31T13:58:47Z")

</div>

thanx for your instant reply. Lemme send the code to you

I have only one controller file named “profile\_controller.rb”, and it  
contains

class ProfileController \< ApplicationController  
@result = MhpProfile.find(:all)  
end

I have 3 Models

“mhp\_friends.rb”, “mhp\_profile.rb”, “mhp\_register.rb”

class MhpFriends \< ActiveRecord::Base  
belongs\_to :MhpProfile  
def self.table\_name() “mhp\_friends” end  
end

class MhpProfile \< ActiveRecord::Base  
has\_many :MhpFriends  
has\_one :MhpRegister  
def self.table\_name() “mhp\_profile” end  
end

class MhpRegister \< ActiveRecord::Base  
belongs\_to :MhpProfile  
def self.table\_name() “mhp\_register” end  
end

I hope this helps. I have only one view under  
app/views/profile/index.rhtml,  
and the first line it has is

\<% @result.each do |res| %\>

which gives an error.

Please help

On Dec 31, 2007 5:30 PM, Jan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> mean
> 
> > What I have done is, I used the generate model script to generate the 3
> 
> wrong, if you don’t show your controller completely (pastie.caboo.se is
> 
> > 
> 
> yanosz
> 
> >

–  
Regards

Haris G.

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [December 31, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/4 "2007-12-31T17:45:28Z")

</div>

On Mon, 31 Dec 2007 16:42:53 +0500, Haris G. wrote:

> 1. I have an existing DB and I want to develop an application that uses  
> this existing DB. How do I import this DB into my application. What I  
> mean is, I dont want to generate new models using rails, I want to use  
> the existing DB.

here’s an example db which has been created by RoR:

thufir@arrakis ~/Desktop/strawr/db $  
thufir@arrakis ~/Desktop/strawr/db $ sqlite3 development.sqlite3  
SQLite version 3.4.1  
Enter “.help” for instructions  
sqlite\> .schema  
CREATE TABLE colors (“id” INTEGER PRIMARY KEY NOT NULL, “feed\_id”  
integer  
DEFAULT NULL, “tag\_id” integer DEFAULT NULL, “color” varchar(255)  
DEFAULT  
NULL);  
CREATE TABLE feeds (“id” INTEGER PRIMARY KEY NOT NULL, “feed” varchar  
(255) DEFAULT NULL);  
CREATE TABLE schema\_info (version integer);  
CREATE TABLE tags (“id” INTEGER PRIMARY KEY NOT NULL, “tag” varchar(255)  
DEFAULT NULL);  
sqlite\> .quit  
thufir@arrakis ~/Desktop/strawr/db $

What does your schema look like? Probably you need to look into a  
getting RoR to work with a legacy db, which is a hassle (at least for  
me).

I don’t advise it until you’ve written at least one RoR app from the  
ground up.

-Thufir

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 1, 2008, 6:12am UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/5 "2008-01-01T06:12:17Z")

</div>

Thanx a lot Jan and Thufir for your help. But now Im getting another  
error  
(and a weird one too). The page doesnt display anything, but the server  
console says

ERROR Errno::ENOBUFS: An operation on a socket could not be performed  
because the system lacked sufficient buffer space or because a queue was  
full.

Any insight into this error ??

On Dec 31, 2007 9:42 PM, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> here’s an example db which has been created by RoR:  
> (255) DEFAULT NULL);
> 
> I don’t advise it until you’ve written at least one RoR app from the  
> ground up.
> 
> -Thufir
> 
> >

–  
Regards

Haris G.

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 1, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/6 "2008-01-01T12:41:21Z")

</div>

Hello,

Am Dienstag, 1. Januar 2008 schrieb Haris G.:

> Thanx a lot Jan and Thufir for your help. But now Im getting another error  
> (and a weird one too). The page doesnt display anything, but the server  
> console says
> 
> ERROR Errno::ENOBUFS: An operation on a socket could not be performed  
> because the system lacked sufficient buffer space or because a queue was  
> full.

Looks like the server has some trouble accessing your DB or processing  
results.  
Is your database queried?  
Did you modify config/database.yml according to your needs?

Keep smiling  
yanosz

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [December 31, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/rails-and-databases/124638/7 "2007-12-31T14:08:08Z")

</div>

Hello,

Am Montag, 31. Dezember 2007 schrieb Haris G.:

> thanx for your instant reply. Lemme send the code to you
> 
> I have only one controller file named “profile\_controller.rb”, and it  
> contains

> class ProfileController \< ApplicationController  
> @result = MhpProfile.find(:all)  
> end

ah, here we go:

controllers need methods for the actions they provide:  
Here:

class ProfileController \< ApplicationController  
def index  
@result = MhpProfile.find(:all)  
end  
end

I guess you should get yourself some kind of docs (books, howtos, etc)

Keep smiling  
yanosz
