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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:53:46+00:00 2026-06-18T11:53:46+00:00

I have both a Customer model and Device model, and the Customer model has_many

  • 0

I have both a Customer model and Device model, and the Customer model has_many :devices and the Device model belongs_to :customer. I’m trying to show either a form to add a device on a customer’s home page if customer.devices.empty? is true or simply show customer’s device and accompanying details if customer.devices.empty is false.

My problem is that customer.devices.empty? is always returning false. With some tests, I’ve seen that customer.devices.count will always display the correct number of devices, however I only get the desired behavior out of customer.devices.empty? while using the Rails console.

I can simply check the value of customer.devices.count, but I would really would like to use the empty? or any? checks as (I think) they are intended.

The problem itself has been described but if you’d like to see code…

   <% if customer.devices.count == 0 %>
     Count is 0 <!-- This is displayed on the page -->
   <% end %>
   <% if customer.devices.empty? %>
     Customer has no devices! <!-- This is NOT displayed on the page -->
   <% end %>
   <% if customer.devices.any? %>
     Customer has <%= pluralize(customer.devices.count, "device") %>.
     <!-- The line above prints "Customer has 0 devices." -->
   <% end %>

Almost forgot my manners — Thanks in advance to any and all answers.

-MM

  • 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-18T11:53:47+00:00Added an answer on June 18, 2026 at 11:53 am

    Use exists? instead of empty?:

    customer.devices.exists?
    

    The difference is that exists? checks the database via the query API, while empty? checks the association contents as a standard Enumerable (which may be dirty/modified).

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

Sidebar

Related Questions

I have Customer, Event and Address objects in my data model. Both Customer and
I have a Job model that belongs_to three users (customer, employee, and qa). The
I have a Customer model with two complex properties InternalAddress and PublicAddress which are
Well i have multiple model and i want to allow the customer to search
Hey have got a page which allows businesses to send invoices to customers (both
I have two models Customer and Referal. Both have an email attribute. Referal belongs
I have both InDesign CS2 and CS3 installed. Both use files with .indd extension.
I have both SQL Server 2005 and Visual Studio 2008 installed and think it
I have both Rails 2.3.4 and Rails 3.0.0.beta installed on my local machine. I
I have both matrices containing only ones and each array has 500 rows and

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.