1

I have a Movie model and an Actor model:

class Movie < ActiveRecord::Base

belongs_to :genre
has_many :reviews 
has_many :actors

end



class Actor < ActiveRecord::Base

belongs_to :movies

end

These are the attributes for each model:

create_table "actors", force: :cascade do |t|
t.string   "name"
t.text     "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer  "movie_id"
end

create_table "movies", force: :cascade do |t|
t.string   "title"
t.integer  "duration"
t.date     "release_date"
t.text     "plot"
t.string   "director"
t.text     "cast"
t.datetime "created_at",   null: false
t.datetime "updated_at",   null: false
end

When a user fills out the form to create a new movie, I want the input from the 'cast' field to save to the Actor model. What action(s) would I need in my controller and what would I need to do in my form?

I've looked at and tried the following and I'm still stuck:

Rails Updating Data in one Model from another Model's Controller

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

How can I update and save one model from another in Rails?

http://railscasts.com/episodes/196-nested-model-form-part-1

Any help is greatly appreciated, thanks!

Community
  • 1
  • 1
Banner
  • 583
  • 2
  • 6
  • 19

1 Answers1

3

You want to create a nested form. You'll need to add accepts_nested_attributes_for :actors to the movie model, then build a subform within the form... Better explanation here:

http://www.theodinproject.com/ruby-on-rails/advanced-forms

Scroll down to "Nested Forms".

spectre6000
  • 455
  • 4
  • 17