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 5995175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:55:22+00:00 2026-05-22T23:55:22+00:00

We start an operation by making sure a customer has enough items with which

  • 0

We start an operation by making sure a customer has enough items with which to work. So we begin by collecting all their current items in an array:

@items = SOrder.where(:user_id => current_user.id).order("order")

Then we determine how many items they should have. If someone has a free account, they should have 5 items. If it is a paid account they should have 20 items:

if current_user.paid
  should_have = 19 # one less than 20 because of 0 position in the array
else
  should_have = 4
end

Then, in case we need to add blank records, we figure out where we should start:

if @items.empty?
  start = 0
else
  start = @items.length + 1
end

If the start is less than or equal to what someone should have, then we add blank records:

if start <= should_have
  value = [start .. should_have].each do |v|
      SOrder.create(:user_id => current_user.id, :order => v, :item_id => 0 )
  end

  @items = SOrder.where(:user_id => current_user.id).order("order") # reload array
end

The records that should be added are not showing up in the database.
Where is the error?

  • 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-05-22T23:55:22+00:00Added an answer on May 22, 2026 at 11:55 pm

    The error may come from calling .length from an Arel object and not a record set.

    @items = SOrder.where(:user_id => current_user.id).order("order").all
    

    However, since you only need a count for the first query, I’d suggest using .count. If I was writing this I’d do something like:

    number_of_items = SOrder.where(:user_id => current_user.id).count
    number_of_blank_items_to_add = current_user.allowed_items - number_of_items
    if number_of_blank_items_to_add > 0
        number_of_blank_items_to_add.times do |num|
            SOrder.create(:user_id => current_user.id, :order => (number_of_items + num), :item_id => 0 )
        end  
    end
    @str_order = SOrder.where(:user_id => current_user.id).order("order")
    

    In User model:

    def allowed_items
       if paid 
           20
       else
           5
       end
    end
    

    Better Yet

    In User model:

    has_many :s_orders, :order => "s_orders.order asc"
    
    def add_extra_blank_orders
        number_of_items = s_orders.count
        number_of_blank_items_to_add = allowed_items - number_of_items
        if number_of_blank_items_to_add > 0
            number_of_blank_items_to_add.times do |num|
                s_orders.create(:order => (number_of_items + num), :item_id => 0 )
        end  
    end
    
    def allowed_items
       if paid 
           20
       else
           5
       end
    end
    

    In controller:

    current_user.add_extra_blank_orders
    @str_order = current_user.s_orders 
    

    While I am sure that you have a good reason, I am questioning why blank items need to be in the database at all. And, if a after_create hook could be used here.

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

Sidebar

Related Questions

I have a page where when some operation goes wrong i want to start
I start build a system with distributed architecture by all applications will communication by
I'm making a thread for my application that's going to do an exit operation
I have an application that has multiple threads processing work from a todo queue.
My application has been successfully updating for ages, and I've been making changes, releasing
A WPF app has an operation of loading a user control from a separate
I have a batch file which performs the operation of listening to the microphone
I've been assigned the task of making our Java web application work with Oracle
I'm making a start on an MVC project, having gone through the MvcMusicStore tutorial.
Let's say I'd like to start a small linux distro before my ordinary operating

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.