Belongs_to and has_many .new issues

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs