I'm using Rails together with Backbone and I've been learning a lot, but I'm having a problem I can't wrap my head around.
I have two models, a User and a Movie model.
ActiveRecord::Schema.define(version: 20141016152516) do
create_table "movies", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_digest"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
The User model has_many movies and the Movies model belongs_to users.
I've been using Backbone collections to add a movie to the view of the User like this,
class Movieseat.Collections.Movieseats extends Backbone.Collection
url: '/api/movies'
defaults:
title: ""
Through my Index view
class Movieseat.Views.MovieseatsIndex extends Backbone.View
template: JST['movieseats/index']
initialize: ->
@collection.on('update', @render, this)
@collection.on('add', @appendEntry, this)
render: ->
$(@el).html(@template())
@collection.each(@appendEntry)
this
events: ->
"click li": "addEntry"
addEntry: (e) ->
movie_title = $(e.target).text()
@collection.create title: movie_title
appendEntry: (entry) ->
view = new Movieseat.Views.Entry(model: entry)
$('#entries').append(view.render().el)
And this is my Entries view
class Movieseat.Views.Entry extends Backbone.View
template: JST['movieseats/entry']
className: 'movie-frame'
render: ->
$(@el).html(@template(entry: @model))
this
This is all working fine. I've got a few movie titles as static text on my page. When a user clicks on a movie title it gets added to the view. But the thing I don't understand is, where does the movie title get saved? And how can I find it using the Rails console?
I tried doing a Movie.all in the console which shows all the movies in the Movie model I made when I used a form to add movies to the model. It does not show the movie titles of the static titles. But if remove has_many :movies from the User model the app crashes.
So what's the connection between User, Movies and Rails and Backbone?