# Belongs\_to and has\_many .new issues

**URL:** <https://www.ruby-forum.com/t/belongs-to-and-has-many-new-issues/145501>\
**Category:** Rails\
**Created:** [August 10, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many-new-issues/145501 "2008-08-10T21:50:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReK2SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ReK2SS](https://www.ruby-forum.com/u/ReK2SS)\
**Post date:** [August 10, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many-new-issues/145501/1 "2008-08-10T21:50:33Z")

</div>

I am new to rails and I been using asociations for 1-2 weeks with no  
issues so far, but this time I need to use them from the start…  
all the examples online are with “already” data in the data base to  
create a new record with the associations working…  
in my case obviosly if you have no data at all only the scheme and I  
want to fill in the form and create a new record that will write to the  
both models associated, but I am trying all possible ways in the console  
before I even bother with the new.html.erb view and im banging my head.  
any help will be apreciated here is the info:

class Calendario \< ActiveRecord::Base  
has\_many :tareas  
end  
class Tarea \< ActiveRecord::Base  
belongs\_to :calendario  
end

ActiveRecord::Schema.define(:version =\> 20080810072747) do

create\_table “calendarios”, :force =\> true do |t|  
t.date “fecha”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
t.time “hora”  
end

create\_table “tareas”, :force =\> true do |t|  
t.integer “calendario\_id”  
t.string “titulo”  
t.text “cuerpo”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

end

in the console I am trying:

tarea.calendario = Calendario.create  
tarea.calendario = Calendario.new  
then the reverse just in case  
calendario.tarea = Tarea.new  
calendario.tarea = Tarea.create  
I as well have try the mothods the association supposed to create with  
no luck  
like  
create\_tarea  
or  
create\_calendario

I know I am doing something very wrong but since I am a noob the mix of  
info from my book and google search is confusing me even more with alike  
situation but none just like mine.

---

<div class="post-metadata">

**Author:** ![ReK2SS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@ReK2SS](https://www.ruby-forum.com/u/ReK2SS)\
**Post date:** [August 11, 2008, 11:44am UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many-new-issues/145501/2 "2008-08-11T11:44:04Z")

</div>

On 10 Aug 2008, at 20:48, ReK2-…-…-…----\> wrote:

> in the console I am trying:
> 
> tarea.calendario = Calendario.create  
> tarea.calendario = Calendario.new

That should work - assuming tarea is an existing instance of Tarea.

> then the reverse just in case  
> calendario.tarea = Tarea.new  
> calendario.tarea = Tarea.create

That would have to be calendatio.tareas (the association name is  
tareas since you wrote has\_many :tareas, so you always use that)  
On top of that it’s a collection (because it’s a has\_many) and so it  
looks like an array, so you should be doing things like

calendario.tareas \<\< Tarea.new  
calendario.tareas = [Tarea.create]  
calendario.tareas.create

(again assuming calendarion is an existing instance of Calendario).

> I as well have try the mothods the association supposed to create with  
> no luck  
> like  
> create\_tarea  
> or  
> create\_calendario

The singular associations give you tarea.create\_calendario. For the  
collection ones you can say calendario.tareas.create - there’s no  
create\_tarea method.

It would help if you said what had happened when you tried the various  
things you listed.

Fred
