I am following the Rails Guide Getting Started with Engines. The only thing that is different is that I am using the RubyMine IDE. I am using ruby 2.5.0 and rails 5.1.5.
Everything has gone smoothly until I reached section 4, "hooking into an application." Before hooking into an application, I wanted to test that all was well. I tried to create a couple articles and comments using both the engine views and also the rails console. I can create articles. But I cannot create comments. I am receiving the error:
SQLite3::SQLException: no such table: main.articles: INSERT INTO "blorgh_comments" ("article_id", "text", "created_at", "updated_at") VALUES (?, ?, ?, ?)
I opened the test/dummy/db/development.sqlite3 database in DB Browser for SQLite and I do indeed have a table named "blorgh_comments".
I'm guessing that the "no such table" error is some kind of "downstream error", i.e., rails can't access the table because of some other (unnamed) error.
I set up the migrations per the guide, the nested resources, the views, and partials. I checked the "special files" mentioned in the guide and they all have the required code (I did not have to change anything).
So I guess the question is, what kind of "underlying" error is likely to trigger this error?
Any help is appreciated.
================ EDIT =================
Engine is now working and I think I know why:
The guide told me to create a comments model with an article_id of type integer (obviously the reference to the parent article), like so:
rails g Comment article_id:integer text:text
Rather than do that, I created the model like so:
rails g Comment article:references text:text
I was unable to create comments, as noted in my question above. When I "refactored" the app and created the migration as instructed in the guide, it worked.
Therefore, I assume that the code that generates rails engines does not recognize and/or properly handle the t.references directive. Specifically, I suspect that it tries to create the index within the change method (of the migration) rather than after it (as is the case in a number of other questions / solutions of a similar nature here on StackOverflow). If I am correct, I think this qualifies as a bug in the implementation of rails engines, but, I won't report it as such because I am not experienced enough to know if that is really the case.
======================== Another Edit =====================
I found this StackOverflow answer by @Igor-Belo that says that it is a name spacing issue. I can use the t.references directive but still need to edit the migration to properly set up the foreign key indexing.