Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:53:43+00:00 2026-06-11T16:53:43+00:00

I am trying to use array functions (specifically, keep_if and include?) within a scope

  • 0

I am trying to use array functions (specifically, “keep_if” and “include?”) within a scope (or self.func_name) for a Rails model, but don’t know how to make it work. I was thinking it was possible because it seems like a “where” implies the “set.where”, so a “set.keep_if” would make sense.

I have a table of documents (or, rather, the metadata for the documents), where each document has different versions of itself in the table, linked; doc.next_version and doc.prior_version. I have a table of people linked to the documents (via another table, ‘author’), so person.documents is a list of all the versions of each document that person worked on. I want to get the first or last version of the document that each person worked on, rather than /every/ version.

Here was my guess as to the code:

class Document < ActiveRecorrd::Base

  has_many :authors

  belongs_to :next_version, :class_name => 'Document'
  has_one :prior_version, :class_name => 'Document'
  #document.document_id is the ID of the prior version of this document

  scope :last!, lambda{ keep_if{|d| (d.next_version.nil?)||(! include?(d.next_version))}}
  # Keep a doc if it either has no next_version (and is thus the last version in 
  # the database), or it's next version isn't in this list.
end

class Person < ActiveRecord::Base
  has_many :authors
  has_many :documents, :through => :authors
end

class Author > ActiveRecord::Base
  belongs_to :person
  belongs_to :document
end

#Usage Example
documents = Person.find(id).documents.last!
published = documents.keep_if{|d| d.published}
success_rate = published.size / documents.size
# etc

I tried converting to a self.first!, but that didn’t help. (I realize that if a person skips a version of a bill, this method won’t skip, and will return two versions of that doc)

I’m looking to find out both more of what’s going on within “scopes”, and how to do what I’m trying to do, even if it uses a completely different method.

I have complete control over pretty much everything, although I’m generating the metadata from plain-text myself – so while I can probably add new metadata fields, I have to do all the work that entails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:53:44+00:00Added an answer on June 11, 2026 at 4:53 pm

    So – What’s going on:

    scopes function like named snippets of a query; you use them to build a query, and ActiveRecord lazily evaluates the resulting composite query. So, from the perspective of scopes and class methods, Person.first.documents isn’t an array, even though it works like one from the perspective of other code – Person.first.documents.keep_if{...}

    The work around is pretty simple – just prompt AREL to evaluate the query and turn into an array:

    def self.last!
      all.keep_if{...}  
     #^^^ It's the "all" that does it
    end
    

    Note, my actual logic in this case, (d.next_version.nil?)||(! all.include?(d.next_version)) doesn’t work, and I’m not yet sure why.

    Edit: It’s something to do with ‘belongs_to :next_version…’, and a def next_version workaround gets things working, if not actually solving the problem.

    Edit 2: I’m going to accept this as the answer for now, because it gets what I want done with the usage code the way I want, but IMO/AFAIK it’s not a very Rails solution – so if you’ve got a better one, I’ll totally jump ship to that one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the SetupDiGetDeviceProperty, but apparently it couldn't find such functions within
Trying to save the $_SESSION['uID'] within the session to use for functions like pulling
I am trying to use an array of functions into my script. I use
I'm trying to use the jQuery $.inArray function to iterate through an array and
I'm trying to use array_filter on an array of objects, and using a public
I'm trying to use an array to set the where parameters for a Zend
I'm trying to use empty() in array mapping in php. I'm getting errors that
I'm trying to use the bubble sort method to sort an array of only
I am trying to use Jquery to iterate through an array of textboxes where
I'm trying to use jQuery's $getJSON to send an array of 'ids'. Here's what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.