# Rails relations

**URL:** <https://www.ruby-forum.com/t/rails-relations/140157>\
**Category:** Rails\
**Created:** [June 11, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/rails-relations/140157 "2008-06-11T13:33:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 11, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/rails-relations/140157/1 "2008-06-11T13:33:54Z")

</div>

Hi ppl.

I am noob in rails and started an app to learn. I have the following  
scenario:

table client with fileds: name, second\_name  
table phone with fields: number, client\_id  
table address with fields: street, number, client\_id

The migrations worked ok and the fields are created.

Now i defined in the model that

for client:  
has\_many :phones  
has\_many :adresses

for phones and adresses:  
belongs\_to :client

I made a form to get some of these fields and add to the database:

\<% form\_for(:clients) do |f| %\>

**Name** \<%= f.text\_field :name %\>

**Second Name** \<%= f.text\_field :second\_name%\>

**Number** \<%= f.text\_field :number%\>

\<%= f.submit "Add" %\>

\<% end %\>

And in the controller i try to save it to the DB:

@clients = Clients.new(params[:clients])  
@clients.save

When i submit i get an error saying that number is not a method, it  
means it  
didnt associate number with a client. It’s not “joining” the tables.

Where did i made a mistake here?

Thanks guys.

–  
Guerra

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 11, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/rails-relations/140157/2 "2008-06-11T14:55:36Z")

</div>

> \<% form\_for(:clients) do |f| %\>
> 
> **Name** \<%= f.text\_field :name %\>
> 
> **Second Name** \<%= f.text\_field :second\_name%\>
> 
> **Number** \<%= f.text\_field :number%\>
> 
> \<%= f.submit "Add" %\>
> 
> \<% end %\>
> 
> And in the controller i try to save it to the DB:
> 
> @clients = Clients.new(params[:clients])  
> @clients.save
> 
> When i submit i get an error saying that number is not a method, it  
> means it  
> didnt associate number with a client. It’s not “joining” the tables.

The way your form is designed, rails is expecting the :number field to  
reside in the model you’re presenting on your form (@client), which it  
isn’t, it’s in an associated model. There are some good railscasts out  
there about “complex forms” (i.e., forms using fields from more than one  
model).

Go forth and google grasshopper.

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 11, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/rails-relations/140157/3 "2008-06-11T15:04:37Z")

</div>

Like this one: [#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73)

And just cause it caught my eye (of course after I posted that last  
comment):

Is your table name is clients, or client? (Tables are generally plural  
in rails)

If it’s a form for a single client, wouldn’t that be  
form\_for(:client) do |f|

an Add is usually for a single entity, and the controller would use

def create  
@client = Client.new(params[:client])  
if @client.save  
blah blah blah

when just picking up rails, it is often easier to follow the rails  
mindset with regards to naming. It makes life much easier.

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 12, 2008, 2:57am UTC](https://www.ruby-forum.com/t/rails-relations/140157/4 "2008-06-12T02:57:46Z")

</div>

Hi Ar, thanks for helping 🙂

I think im using the correct names.

I watcher the cool railcasts and managed to create the form to get more  
than  
one number per user for instance.

The problem is that when i check the database, it doesnt create a  
foreign  
key for the numbers i add.

It simply adds null in the foreign key fields.

I would like to know where i did a mistake in my relations.

Thanks,

On Wed, Jun 11, 2008 at 10:04 AM, Ar Chron  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## form\_for(:client) do |f|
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Guerra

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 12, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/rails-relations/140157/5 "2008-06-12T14:35:59Z")

</div>

Israel G. wrote:

> I would like to know where i did a mistake in my relations.

Hrmm… post your current form and controller code?

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 15, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/rails-relations/140157/6 "2008-06-15T23:55:09Z")

</div>

Here are my 4 files.

I have the clientes, cliente\_telefone and cliente\_endereco models.

clientes has\_many cliente\_telefones and cliente\_enderecos. and each of  
these  
(telefones and enderecos) belongs to telefone.

In the controller for clientes i create a form for 3 telefones for a  
cliente. After i run it, the cliente\_telefone data is in the DB, but  
with  
the foreign key fields with NULL.

How can i make rails create it correctlly?

On Thu, Jun 12, 2008 at 9:35 AM, Ar Chron  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

–  
Guerra

---

<div class="post-metadata">

**Author:** ![Israel\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@Israel\_G](https://www.ruby-forum.com/u/Israel_G)\
**Post date:** [June 16, 2008, 1:03am UTC](https://www.ruby-forum.com/t/rails-relations/140157/7 "2008-06-16T01:03:50Z")

</div>

Just found the problem.

When i created the migration, the name of the foreign key field was not  
in  
plural.

Thanks anyway!

On Sun, Jun 15, 2008 at 6:54 PM, Israel G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ## Hrmm… post your current form and controller code?
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > > >
> 
> –  
> Guerra

–  
Guerra
