How do I "unscope" an edit path and/or action in Rails 3?

580 Views Asked by At

Currently I'm working on a simple Rails 3 web application with devise. When users sign up an Account is created. This the essence of the Account model:

class Account < ActiveRecord::Base
  include Authority::UserAbilities

  # Callbacks
  after_create do |account|
      account.create_person
      account.add_role :owner, account.person
  end

  has_one :person, dependent: :destroy
end

And the essence of the Person model:

class Person < ActiveRecord::Base
  belongs_to :account
  default_scope where(published: true)
end

As you can see a person (which basically is a user profile) is created after the creation of an account, the default value of published is false. After signing up the user is signed in and redirected to the home page, which contains edit_person_path(current_account.person).

After setting the default_scope for Person a Routing Error: No route matches {:action=>"edit", :controller=>"people", :id=>nil} was thrown because of edit_person_path(current_account.person).

My solution to this was to change edit_person_path(current_account.person) into edit_person_path(Person.unscoped { current_account.person} ).

The issue that I'm having now is that although the page is rendering fine, when I click on the edit link the following exception is raised:

ActiveRecord::RecordNotFound in PeopleController#edit
Couldn't find Person with id=126 [WHERE "people"."published" = 't']

What's the best way to temporarily unscope the edit action, should I do this in PeopleController#edit?

0

There are 0 best solutions below