I'm trying to create a few simple relations in my Rails 4 application. I have three models: user, list, word.
This is for a simple 'list' application, where each user has_many words through lists.
I currently have the following associations...
User model
class User < ActiveRecord::Base
has_many :lists
has_many :words, through: :lists
end
List model
class List < ActiveRecord::Base
has_many :words
belongs_to :user
end
Word model
class Word < ActiveRecord::Base
belongs_to :list
end
DB Scheme
ActiveRecord::Schema.define(version: 20150320200247) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "lists", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "words", force: :cascade do |t|
t.string "word"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "words", ["word"], name: "index_words_on_word", using: :btree
end
creating objects:
> user = User.create(name: "Kyle")
> list = List.create(name: "List One")
> word = Word.create(word: "StackOverflow")
# Display words in list
> list.words
The above association object gives an error: PG::UndefinedColumn: ERROR: column words.list_id does not exist.
I've also tried creating migrations with foreign key constraints using the Foreigner gem, but am still getting errors. Please help!
list_idin yourwordstable anduser_idin theliststable, these are required for the association to work correctly.