Likes routing error
I am trying to use the Likes tutorial to create Favs for locations, which are nested under stores.
Here is the relevant route section
resources :stores do
resources :locations do
resource :favorite, module: :locations
end
end
It should be noted I am using Friendly ID and that works for my stores and locations currently.
Error
Favoriting works however un-fav throws the following error.
SQLite3::SQLException: no such column: favorites.: DELETE FROM "favorites" WHERE "favorites"."" = ?
Relationships
app/models/location.rb
has_many :favorites
has_many :users, :through => :favorites, :source => :user
app/models/user.rb
has_many :favorites
has_many :locations, :through => :favorites, :source => :location
I would real appreciate any help you could provide.
Hmm, it looks like you've somehow got a bad query. When it says WHERE "favorites".""
there should be something inside those last two empty quotes.
What does your action looks like?
Here is favorites#destroy
def destroy
@location.favorites.where(user_id: current_user.id).destroy_all
respond_to do |format|
format.html {redirect_to store_location_path(@store, @location)}
format.js
end
end
That looks correct to me. You may want to run this in your Rails console to verify it is working outside of your action. I can't think of what would cause it to generate the WHERE without a column name there, but it has to be something revolving around your database, associations, or query.
Another thing, have you verified that @location.favorites works correctly? When you hit the create, it makes the record correctly including the user_id?
Chris, thanks for the quick replies!
I open rails console and verified a favorite. Checked my user id, checked the location id, verified that location.first.favorites
works showing the first user having a favorite of the first location.
The interesting part is that I cannot delete a favorite in the console. I figured maybe I set up wrong originally so I Would delete all favorites and start fresh. Inside console:
Favorites.all
lists my favorites, but
Favorites.destroy_all
throws the exact same error as the site
SQLite3::SQLException: no such column: favorites.: DELETE FROM "favorites" WHERE "favorites"."" = ?
Would you mind double checking my relationships included in the initial post, this relationship setup is something I have never used before, specifically the "source" portions. Google searches turned up a lot of solutions resulting in correcting relationship setup.
Also here is my *schema.rb. Figured this may be beneficial as the last error would make me think its some sort of database issue. This seems very straight forward to me though.
create_table "favorites", id: false, force: true do |t|
t.integer "user_id"
t.integer "location_id"
end
add_index "favorites", ["location_id"], name: "index_favorites_on_location_id"
add_index "favorites", ["user_id"], name: "index_favorites_on_user_id"
Also final thought: Where should the favorite model be located? This route setup where using favorites as a module is also new for me. Currently, this file is located in app/models/favorites should this be inside a locations folder?
Thanks I really appreciate your help especially considering its a weekend.
Always happy to help! :) Plus, weekends are when I record screencasts anyways.
When you use a route module, it just means to put the controller in a folder of that name. If you say module: :locations
then it will look in app/controllers/locations
for the favorites_controller.rb
file. It doesn't affect your models at all.
The thing here I noticed is that you have create_table without an ID column. That could be affecting the query. It shouldn't matter, but Rails usually wants a primary key to look up the records quickly. Try generating the table again with the ID column and see what happens.
Another possibility is you might try generating the model named singularly instead as "Favorite" instead of "Favorites". That is also something that Rails often wants a certain way and deviating can cause it to do some unexpected things once in a while.
Regenerating the table with ID fixed this issue. THANKS! I could have sworn I read somewhere that was the best way to create join tables, clearly it causes issue though.
Also for clarity, my model is favorite, the "s" was a typo.
Thanks again!!! Really appreciate it. I really enjoy your tutorials, please keep them coming!
Haha! Glad it's working for you. I think you're right about the join table not needing a primary key, but I think when you do that in Rails, it assumes you don't create a model for it. Since we're interacting with that join table a bit differently than normal, it seems like it probably is a good case to have it. In general, a ID column doesn't really add any noticeable overhead to your database, so you don't have to worry about that in the future.