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

  • SEARCH
  • Home
  • 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 8987813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:52:50+00:00 2026-06-15T21:52:50+00:00

I have a Cover model, in the cover.rb file, I also define a method

  • 0

I have a Cover model, in the cover.rb file, I also define a method called size which returns an integer representing ‘small, intermediate, large’.
My question is how can I retrieve all the small/intermediate/large covers?
My guess is to use scope, but I cannot figure out how to pass the size method as a condition.

class Cover < ActiveRecord::Base
  attr_accessible :length, :width

  # TODO
  scope :small
  scope :intermediate
  scope :large

  # I have simplified the method for clarity.
  # 0 - small; 1 - intermediate;  2 - large
  def size
    std_length = std_width = 15
    if length < std_length && width < std_width
      0
    elsif length > std_length && width > std_width
      2
    else
      1
    end
  end

end
  • 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-15T21:52:52+00:00Added an answer on June 15, 2026 at 9:52 pm

    This could work:

    class Cover < ActiveRecord::Base
      attr_accessible :length, :width
    
      scope :of_size, lambda{ |size| 
                              case size
                                when :small
                                  where('width < 15 AND height < 15')
                                when :large
                                  where('width > 15 AND height > 15')
                                when :intermediate
                                  where('(width < 15 AND height > 15) OR (width > 15 AND height < 15)')
                                else
                                  where(id: -1) # workaround to return empty AR::Relation
                            }
    
      def size
        std_length = std_width = 15
        return :small if length < std_length && width < std_width
        return :large if length > std_length && width > std_width
        return :intermediate
      end
    
    end
    

    And use it like this:

    Cover.of_size(:small) # => returns an array of Cover with size == small
    

    To make it work with several arguments:

    # in the model:
    scope :of_size, lambda{ |*size| all.select{ |cover| [size].flatten.include?(cover.size) } }
    # how to call it:
    Cover.of_size(:small, :large) # returns an array of covers with Large OR Small size
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Tour model with a paperclip 'cover' attachment. When the Tour model
I have a Client model which has many projects. In the project model I
I have built a modal box, which uses a cover-all div background to fade
I have data that looks like this: model aspect cover contour 1 flowering ~
I currently have a complete cover background image. Here is the code: #back{ /*
I have two CSS tranistions as below (they both cover the page and then
Say I have a loop of objects (style.cover.pic) in a DIV .style_image <div class=style_image>
I have a JasperReport and I want to create for this report a cover
I have a photo gallery I'm trying to set a picture as the cover
I am a bit new to the Devel::Cover module, but have found it very

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.