Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to specify primary key #35

Closed
MikeAbner opened this issue May 13, 2011 · 8 comments
Closed

No way to specify primary key #35

MikeAbner opened this issue May 13, 2011 · 8 comments

Comments

@MikeAbner
Copy link

Don't see how to set the primary key column. I have UUID as the primary key of a few tables.

@gregbell
Copy link
Contributor

You should be able to implement this using the to_param and using a before filter in the resource.

I haven't actually tested this, so let me know if this works... But it should look something like this.

class Post < ActiveRecord::Base
  def to_param
    uuid
  end
end

ActiveAdmin.register Post do

  before_filter :only => [:show, :edit, :update] do
    @post = Post.find_by_uuid(@post)
  end

end

Active Admin should use the @post variable instead of looking it up if you have already defined it.

Again, I haven't had a chance to test this out, but we use this method for show screens in our app. I'm assuming it will work for the edit and update.

Let me know how it goes.

@jeffrafter
Copy link

Actually this should read:

before_filter :only => [:show, :edit, :update] do
   @post = Post.find_by_uuid!(params[:id])
end

Adding the ! gives you the nice 404 behavior, and @post is not defined at this point, params[:id] is.

@gregbell
Copy link
Contributor

@malyk did this work for you?

@MikeAbner
Copy link
Author

Greg

I actually never tried it. Got caught up on other things. If I do give it another go (which I do want to do when time permits) I'll let you know. Thanks for looking into it.

@gregbell
Copy link
Contributor

Cool. Thanks @malyk

@sbeam
Copy link

sbeam commented Sep 1, 2011

I found a way to do this that leverages i_r's :finder option on the controller. I am using stringex and have a slug column on the Model. So

ActiveAdmin.register Category do
    controller do
        defaults :finder => :find_by_slug
    end
end

@makvool
Copy link

makvool commented Oct 12, 2011

thanks @sbeam, worked like a charm

@mitchwd
Copy link

mitchwd commented Nov 14, 2011

Thanks @sbeam. Was looking for a while!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants