11

Ok again forget my noobiness in Rails, but I'm not finding this either. Although I think this one might be because well.. it doesn't exist?

Ok so I'm still playing with RoR and I added a controller for employee, pretty simple just a name, clock_no, and wage. But what if I don't want to search for an employee by ID? To me it would be easier for the user to search by clock number, but I'm not figuring out how that works. I am assuming it has something to do with this line, more specificly the set_employee line:

private # Use callbacks to share common setup or constraints between actions. def set_employee @employee = Employee.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def employee_params params.require(:employee).permit(:name, :clock_no, :wage) end 

However I could be way off.

Ok so my solution ended up being just to change the primary_key in the model:

self.primary_key = 'clock_no' 

And setting the return to json:

def show @employee = Employee.find(params[:id]) respond_to do |format| format.html format.json { render json: @employee } end end 

HOWEVER, I don't think this is really the end all solution, again my noobiness could be showing, but it seems kinda hockie to me. My hold up would be what if you wanted to search by say name or something else that wasn't the primary key and was not able to change the primary key like in my case? nose back to the docs

3 Answers 3

22

Use where:

Employee.where(clock_no: params[:clock_no]) 

where returns an ActiveRecord relation, so if you need the first (and probably only instance if clock_no is unique), use first, like this:

Employee.where(clock_no: params[:clock_no]).first 

This will execute only one SQL query to get the first Employee matching clock_no.

There used to be magic finders like find_by_clock_no, but those are deprecated as of Rails 4, which I assume you're using.

Also see the Rails documentation for more info on ActiveRecord queries.

Sign up to request clarification or add additional context in comments.

6 Comments

You're right, I keep forgetting that these dynamic finders are gone in the new rails. Thanks!
I just looked at the docs, and they're actually deprecated in Rails 4, and will be gone in 4.1. Thought I read somewhere they would be removed in 4 already, but guess I was wrong. Not using them anymore is best practice anyway ;)
Yes indeed, I've deleted my answer. Yours is the best.
Ah ok that makes sense. I'll take a look at that when I get to work. Thanks @fivedigit
Ok guys I was able to get it to return json, but now it's showing nil unless I set Employee.where(clock_no: '1684'). Then it returns the data from the server in json format. Is there something else I could be missing? Do you need me to edit and post current code?
|
4

Try using a dynamic attribute-based finder, like so:

 Employee.find_by_clock_no(params[:clock_no])

check the docs for more info.

1 Comment

deprecated as of Rails 4
1

I think nowadays the next syntax is preferred:

Employee.find_by(clock_no: params[:clock_no]) 

1 Comment

Nice, this also generates a 404 reply if no data is found.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.