Could not find record error while deleting record in Rails - ruby-on-rails

I'm trying to delete a record by passing id of that record. The code looks like this:
def destroy_catalogue_entry
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
if #catalogue_entry.destroy
flash[:success] = 'Catalogue entry deleted successfully.'
else
flash[:error] = 'Failed...'
end
end
I'm getting an interesting error. When my function destroy_catalogue_entry is called it shows:
Couldn't find CatalogueEntry with 'id'=16
but as I comment If condition section and render #catalogue_entry as json, the output is printed successfully. So how is it possible? Am I making some silly mistake or is there logical reason. Please enlighten me.

Solved! All I did is this:
def destroy_catalogue_entry
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
if #catalogue_entry.destroy
flash[:success] = 'Catalogue entry deleted Successfully'
redirect_to action: :view_catalogue_entries, dc_id: #catalogue_entry.dc_id
else
flash[:success] = 'Failed...'
end
end
When I notice the console, the record was getting deleted successfully but after that there was a SELECT query for the same record, that is why it was throwing the error Couldn't find CatalogueEntry with 'id'=16. As I redirected it, the problem was solved.

I think destroy method is returning an object. In ruby anything other than false or null will be taken to true in if statement. You can do puts on destroy method and see what its returning.

i presume your,
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
is returning that error because it cant find CatalogueEntry with id 6, make sure you have CatalogueEntry with that id.

Related

Rails - Not hitting while loop when creating classes

I have a create method in Rails where I am trying to create multiple objects in a while loop. For some reason it doesn't seem to be hitting the while loop so no objects are being created. The code is below:
def create
#user = User.find(params[:participant][:user_id])
#activity = Activity.find(params[:activity_id])
weeks = #activity.weeks
i = 1
while i <= weeks do
puts "Test"
participant = Participant.new
participant.user_id = #user.id
participant.activity_id = #activity.id
participant.attended = false
participant.paid = false
participant.week = i
participant.save
i = i+1
end
redirect_to user_activities_path(#user, :id => #activity.id)
end
The form I am using to submit is working fine as I can see from the console, and the redirect_to method at the end is working, so it just seems to be missing the loop. If it helps, the value of weeks is 10. Any help would be greatly appreciated.
If multiple Test have been output, try participant.save!, i think the participant save might fail, like some column not valid, so no objects are being created.
Please check if activity record is get fetched. I think your 3rd statement should be as follow.
#activity = Activity.find(params[:participant][:activity_id])

How can I set a variable to a value, only if that value is valid?

I want to set a variable to a given value, but only if the value is valid.
Right now this is the code I have:
if Something.find(params[:id].comments.first.exists?
#comment = Something.find(params[:id]).comments.first
else
#comment = nil
end
But this is inefficient because it has to load the records twice.
I tried to use the ruby method try to ensure that the variable would only be set if the value is valid:
#comment = Something.try.find(params[:id]).comments.first
but no matter where I put it, I get back a "nil is not a symbol" error. It seems try is only for printing variables.
Anyone know how else I can accomplish this with only one query?
You could try:
#comment = Comment.find_by(something_id: params[:id])
Did you tried?
if #comment = Something.find(params[:id]).comments.first
# do something with your #comment variable
else
# do something else
end
More examples: Check if record exists from controller in Rails
# Attempt your query
#comment = Something.find(params[:id]).comments.first
# If the record does not exist, use rescue to handle the exception gracefully
rescue ActiveRecord::RecordNotFound
# Handle the exception
#comment = nil
end
You can find more information on exceptions and exception handling here.
I think this is what you want:
#comment = Something.find_by(id: params[:id]).try(:comments).try(:first)
In this case find_by will return a Something object or nil. In case of nil, the chain of tries will still return nil, and, in case of object, methods in tries will be executed and give you the first comment, if such exists (note! you might still get a nil if object exists because that object might not have any comments).
Thus, you will set your #comment only if Something object exists and it has comments; otherwise, it will be nil.

Add information to DB from the controller on rails

I'm trying to add the user id to 2 columns in the db after a user successfully submits a form. The commands run fine in the console but wont work in the controller. I haven't added info to the db straight from the controller before so I'm thinking that I'm doing something wrong.
Here is the create method
def create
#game = Game.friendly.find(params[:game_id])
#game_category = Game.friendly.find(#game.id).game_categories.new(game_category_params)
if current_user.mod_of_game? params[:game_id] && #game_category.save
Game.find(#game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(#game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
flash[:info] = "Game category added succesfully!"
redirect_to #game
else
render 'new'
end
end
These 2 lines are supposed to add the user id to the submitted_by and approved_by columns but they don't. I don't get any error messages, they simply just don't add anything to those columns
Game.find(#game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(#game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
If I replace the lines with coding that works in the console to see if its a variable or something thats not right it still doesn't work
Game.find(12).game_categories.find(55).update(submitted_by: 1)
Game.find(12).game_categories.find(55).update(approved_by: 1)
I'm building an app to learn rails and I guess this is something I just don't know.
Can anyone enlighten me on what I'm doing wrong?
Update:
Ok it is now giving me an error - Couldn't find GameCategory without an ID
So the #game_category.id isn't working?
It is a small typo in your query you missed #.
Game.find(#game.id).game_categories.find(#game_category.id).update(submitted_by: current_user.id)
After playing around tweaking it here and there it turns out to be this line
if current_user.mod_of_game? params[:game_id] && #game_category.save
when changed to this it works
if #game_category.save && current_user.mod_of_game? #game

Rails Activerecord: Update where conditions... else create

I need to check multiple columns of a table to see if I find a match. If I find a match I need to "updateattributes" of the matching record with all of my form params... Else I need to add a new record with all of my form params.
if #somethingtoupdate = Model.where("column1 = ? and column2 = ?", params[:something][:column1], params[:something][:column2])
if #somethingtoupdate = Model.update_attributes(params[:something])
redirect_to somewhere_path, :notice => "The existing record was updated"
else
render "myformlocation"
end
else
#added = Model.new(params[:something])
if #added.save
redirect_to somewhere_path, :notice => "The new record was created"
else
render "myformlocation"
end
end
Update
#somethingtoupdate = Model.where("this_id = ? and that_id = ?", params[:something][:this_id], params[:something][:that_id])
if ! #somethingtoupdate.empty?
if #somethingtoupdate.update_attributes(params[:something])
redirect_to some_path, :notice => "The existing record was updated"
else
render "myformlocation"
end
else
#added = Model.new(params[:something])
if #added.save
redirect_to some_path, :notice => "The new record was created"
else
render "myformlocation"
end
end
This is where I stand now thanks to #micahbf.
But, I am still getting an error on my "update_attributes" when there is a matching record.
Seems like this should work.... What am I missing or doing wrong?
This is because where does not return nil if it doesn't find anything, it returns an empty array, which is still truthy, so the block gets executed.
You can use empty? to check whether to run the block or not.
Note also that if it finds a match, the match will still be returned inside of an array (even if there was only one match). So you will have to do something like call first on the result to take the first returned model and update it.
So, the top might look like:
#somethingtoupdate = Model.where("column1 = ? and column2 = ?", params[:something][:column1], params[:something][:column2])
if ! #somethingtoupdate.empty?
if #somethingtoupdate.first.update_attributes(params[:something])
redirect_to some_path, :notice => "The existing record was updated"
else
render "myformlocation"
end
else
// do stuff if the query found no matches
end
I think here is short method to find record and if found then update record and if record not found then create it.
#somethingtoupdate = Model.where("column1 = ? and column2 = ?", params[:something][:column1], params[:something][:column2]).first_or_initialize
#somethingtoupdate.update_attributes(params[:something])
First of all, Model.update_attributes(params[:something]) is not working (at least in Rails 3.2.12). It should be #somethingtoupdate.update_attributes(params[:something]).
Also, there is an existing method for this kind of purpose: first_or_create.
#somethingtoupdate = Model.where("column1 = ? and column2 = ?", params[:something][:column1], params[:something][:column2]).first_or_create

Form parameter isn't saving properly in Rails

So, I've got this action in order to save a model:
def create
logger.debug(params[:user_id])
group_id = params['approver']['group_id']
#approver = Approver.new(params[:approver])
#approver.user_id = params[:user_id]
respond_to do |format|
if #approver.save
logger.warn("Approver saved!")
flash[:notice] = "New approver has been added!"
format.html { redirect_to(group_path(group_id)) }
else
flash[:notice] = "Sorry .. had issues adding the approvers!"
format.html { redirect_to(group_path(group_id)) }
end
end
end
Parameters being passed in are:
Parameters: {"commit"=>"Submit", "authenticity_token"=>"C35lovRRjJzekruZiwTZjaMs4KgwiEJnXn10b0nD+0w=", "utf8"=>"✓", "user_id"=>["18"], "approver"=>{"group_id"=>"13"}}
And looking at my logs, the debug message in the action prints '13' as the correct value. However, the value being inserted into the database is always '1' and here's the snippet from the logs:
INSERT INTO `approvers` (`user_id`, `updated_at`, `created_at`, `group_id`) VALUES (1, '2011-07-13 04:58:51', '2011-07-13 04:58:51', 13)
To further complicate matters, in order to debug, if I change line 5 of the action to:
#approver.user_id = 19
it all works fine.
Can anyone explain what I'm doing wrong?
The group_id isn't your problem, that is going through fine. Your problem is this:
"user_id"=>["18"]
So params[:user_id] is actually an array but you're treating it like an ID. Then, someone inside Rails is converting that array to 1. When you say this:
#approver.user_id = 19
Everything works because you're assigning a Fixnum to user_id and that's what user_id expects.
You need to figure out why you're getting an array user_id and then fix that. Or, figure out what you should do with an array, maybe #approver.user_id = params[:user_id][0] makes sense, maybe not.

Resources