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

ActiveRecord should provide a convenient way for a model object to exclude itself from a scope #8297

Closed
betesh opened this issue Nov 23, 2012 · 3 comments

Comments

@betesh
Copy link
Contributor

betesh commented Nov 23, 2012

In addition to handling an exception thrown when uniqueness is validated upon saving a record, I often find it necessary to check whether a given record has some unique attribute when no writing to the database is necessary. The code usually looks something like this:

class MyModel < ActiveRecord::Base
  def  all_other_records_in_some_class_method_or_scope
    return self.class.some_class_method_or_scope if self.new_record?
    self.class.with_scope(self.class.where('id != ?', self.id)) do
      self.class.some_class_method_or_scope
    end
  end
end

(The reason for the early return when new_record? == true is that self.id doesn't usually behave nicely for unsaved records.)

ActiveRecord::Base could encapsulate this logic so that this code would be reduced to the following:

class ActiveRecord::Base
  def  all_other_records_in scope
    return self.class.__send__(scope) if self.new_record?
    self.class.with_scope(self.class.where('id != ?', self.id)) do
      self.class.__send__(scope)
    end
  end
end
class MyModel < ActiveRecord::Base
  def  all_other_records_in_some_class_method_or_scope
    all_other_records_in :some_class_method_or_scope
  end
end

Note that the scope argument need not be a scope. Any class method with any return type should be valid.
I submit this feature to the Rails team because I strongly suspect my implementation is far from optimal but don't know my way around ActiveRecord well enough to know of a better solution.

@carlosantoniodasilva
Copy link
Member

Please send your feature request to the Rails Core mailing list, or try to propose it in a pull request, to gather more people discussing about it. Lets keep the issues tracker for issues ;). Thanks!

@betesh
Copy link
Contributor Author

betesh commented Nov 23, 2012

Sorry, didn't realize I could start with a PR without creating an issue to
track it against first.

On Fri, Nov 23, 2012 at 5:31 AM, Carlos Antonio da Silva <
notifications@github.com> wrote:

Please send your feature request to the Rails Core mailing list, or try to
propose it in a pull request, to gather more people discussing about it.
Lets keep the issues tracker for issues ;). Thanks!


Reply to this email directly or view it on GitHubhttps://github.com//issues/8297#issuecomment-10655642.

@carlosantoniodasilva
Copy link
Member

@betesh no problem. The best ways to initiate a discussion about a new feature is to either try the rails core mailing list to get some feedback first, or go straight to the code and put up a pull request showing your intents. When in doubt, the core list + some code examples is the best to start with.

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

2 participants