I have been working on a small app and at some point while creating a new posting I wanted my app to check the database for certain text value, specifically looking for “completed”.
So far I came up with this;
@job = current_user.Jobs.where(:all, :project_status => "completed")
if @job.exists?
do something
else
send an error
end
that was completely wrong, after reading up a little on it and going through few examples I think the following does the same thing;
@job = current_user.Jobs.exists?(:conditions = {:project_status => "completed"})
but I haven’t found an example in my research where the above line goes into “if” condition, do I just do;
@job = current_user.Jobs.exists?(:conditions = {:project_status => "completed"})
if @job
do something here
else
do something else here
end
would that be shorter, cleaner and correct?
1 Answer