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 scope with first returns all records #13647
Comments
Scopes must return relation objects, so we are not supposed to be returning the objects themselves with first/last/find or any other. The idea behind falling back to returning class Post < ActiveRecord::Base
scope :by_author, -> author { where(author: author) if author }
scope :by_tag, -> tag { where(tag: tag) if tag }
end
Post.by_author(params[:author]).by_tag(params[:tag]) In case Check out this post for more details on scopes, and the api docs for scope. Thanks! /cc @rafaelfranca this might be of your interest :) |
@carlosantoniodasilva what if |
@tak2siva the important part is that it's going to return a relation, which will later be fetched (not inside the scope), and whether it's gonna return 0 or 1 or 10 records shouldn't affect the expected behavior. However, returning something that's not a relation, ie with |
Hi there. I recently ran into the same issue, and while I understand the intent, I think the current implementation of Below are details on each of those points. I would be happy to hear your thoughts on them @carlosantoniodasilva. Always a relationGiven this model class Post < ActiveRecord::Base
scope :most_recent, -> { where(status: 'published').first }
end If there are no published post, the result of the scope will be However, if there is one, the result of the scope will be an instance of DangerousSince the Syntactic sugarSince |
I have a same issue related to scope.this is my code |
Basically, Should this be added to the documentation? Also, should the doc for Rails 4.2 be updated with
given this behaviour is there as well? |
I want to create a scope that says |
This is the correct code I have so far |
@ChristinaXT it sounds like you're ordering records by name in descending order, which doesn't quite match the "users with most requests" scope name... feels like something is missing. Do you have multiple models you're trying to connect here to achieve this "most requests" logic? You can always apply a |
Yes! the second part of a combination is exactly what I need. I just
haven't figured it out yet. It has to be a scope method though. (it is a
code challenge on my rails project.)
Best Regards,
Christina Williams
LinkedIn <https://www.linkedin.com/in/christina-m-williams-6591b3132/>
…On Fri, Jun 14, 2019 at 2:59 PM Carlos Antonio da Silva < ***@***.***> wrote:
@ChristinaXT <https://github.com/ChristinaXT> it sounds like you're
ordering records by name in descending order, which doesn't quite match the
"users with most requests" scope name... feels like something is missing.
Do you have multiple models you're trying to connect here to achieve this
"most requests" logic?
You can always apply a limit to the relation, for example order().limit(X),
but that still returns a relation that can be used in the scope.
"relation" basically means a collection of records. If you want a single
*record*, I think you're looking for a combination of the scope (to build
the order / condition you need), and then take (or first/last) to fetch
that single record from the scope/relation. I hope that helps.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#13647?email_source=notifications&email_token=AJRWMY5CO4R5JTBMFQJZSALP2PS73A5CNFSM4ALBYSKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXXVUPY#issuecomment-502225471>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJRWMY3X3SD72NEBIBOZEOLP2PS73ANCNFSM4ALBYSKA>
.
|
I would like to understand better why # activerecord/lib/active_record/relation/query_methods.rb
# Returns a chainable relation with zero records.
#
# The returned relation implements the Null Object pattern. It is an
# object with defined null behavior and always returns an empty array of
# records without querying the database.
#
# Any subsequent condition chained to the returned relation will continue
# generating an empty relation and will not fire any query to the database.
#
# Used in cases where a method or scope could return zero records but the
# result needs to be chainable.
#
# For example:
#
# @posts = current_user.visible_posts.where(name: params[:name])
# # the visible_posts method is expected to return a chainable Relation
#
# def visible_posts
# case role
# when 'Country Manager'
# Post.where(country: country)
# when 'Reviewer'
# Post.published
# when 'Bad User'
# Post.none # It can't be chained if [] is returned.
# end
# end
#
def none
spawn.none!
end |
This is the model I have
On accessing fail, I get unexpected results(i.e it retrieves all records of TestModel ) instead of returning nil
I'm using Rails 4.0 and MySQL 5.1
The text was updated successfully, but these errors were encountered: