0

I have a strange SQLException with my association.

I`ve created a join table using migration:

create_join_table :reviews, :posts

So there is a table in db posts_reviews

In both models I`ve configured relations:

class Post < ActiveRecord::Base
    has_and_belongs_to_many :reviews


class Review < ActiveRecord::Base
    has_and_belongs_to_many :posts

But when I run this command Post.find(X).reviews << Review.find(Y) The result is:

SQLite3::SQLException: no such table: : INSERT INTO "" ("post_id", "review_id") VALUES (?, ?)

So the join table name is empty.

My schema.rb

create_table "posts", force: true do |t|
  t.integer  "user_id"
  t.string   "post_title",     limit: 1024
  t.text     "post_body"
  t.boolean  "promo_material",              default: false
  t.integer  "views_count",                 default: 0
  t.integer  "comments_count",              default: 0
  t.integer  "votes_count",                 default: 0
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "main_image"
end

create_table "posts_reviews", id: false, force: true do |t|
  t.integer "review_id", null: false
  t.integer "post_id",   null: false
end

create_table "reviews", force: true do |t|
  t.integer  "user_id"
  t.integer  "composition_id"
  t.text     "review_text"
  t.boolean  "promo_material", default: false
  t.integer  "rating",         default: 0
  t.integer  "views_count",    default: 0
  t.integer  "comments_count", default: 0
  t.integer  "votes_count",    default: 0
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "promo_image"
end
Maxim
  • 158
  • 2
  • 12
  • Can you post the migration file for the table? – RichardAE Feb 18 '15 at 10:09
  • Also please post your `schema.rb` file. – nicohvi Feb 18 '15 at 10:13
  • @nicohvi, I have added `schema.rb` fragment. @RichardAE, My migrations is very simple `create_join_table :reviews, :posts` – Maxim Feb 18 '15 at 11:35
  • Great! And you've run `rake db:create` and `rake db:migrate` without problems? – nicohvi Feb 18 '15 at 11:39
  • Also, it looks from your statement that the table hasn't been created yet (`INSERT INTO ""`), so please ensure that you've run all migrations. – nicohvi Feb 18 '15 at 11:47
  • @nicohvi I have just recreated db and run all migrations without any problems. The table exists in database, I could see it using sqlite manager. Also If I insert rows manually then rails association works normal. I can get models like `Post.find(X).reviews`. But deleting and inserting into collection doesn`t work. – Maxim Feb 18 '15 at 14:08
  • So if you run `rails c` you can do `Post.find(X).reviews` but you can't do `Post.find(X).reviews << Reviews.find(Y)`? – nicohvi Feb 18 '15 at 15:29
  • @nicohvi, yes that is right. I can not add record to the collection( – Maxim Mar 24 '15 at 13:40

0 Answers0