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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:20:03+00:00 2026-06-07T12:20:03+00:00

I have a table ( Dishes ) that has a column ( nutrition_facts )

  • 0

I have a table (Dishes) that has a column (nutrition_facts) that stores key/value data (similar to hstore in a PostgreSQL database).

I’ve defined a new instance of a Dish in my fixtures folder as such:

nutrition_facts:
  id: 6
  name: Dish 6
  category: Cat 1
  description: Something about Dish 6
  price: 15.32
  menu_id: 1
  nutrition_facts:
    serving_size:
    calories: 392

But I can’t seem to make the following assertions work when I run rake test:units — it fails on the first assert_not_nil

  test "no serving size with calories" do
    dish = dishes(:nutrition_facts)

    assert_not_nil(dish.calories)
    assert_nil(dish.serving_size)
    assert !dish.save, "Saved dish when serving size wasn't defined while calories was"
  end

How can I test for the individual calories and serving_size values without testing the entire nutrition_facts field?

In short, I’m trying to test the following validation (from my dish.rb file), which works when I run the application in a browser:

  # Check 'serving size' isn't blank only if 'calories' is defined
  validates_presence_of :serving_size, :if => Proc.new { |val| !val.calories.blank? }
  • 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-07T12:20:05+00:00Added an answer on June 7, 2026 at 12:20 pm

    I was able to get it to work by doing defining my nutrition_facts column’s key/value as such:

    nutrition_facts: <%= {:serving_size => nil, :calories => 33}.to_yaml.inspect %>
    

    So in my fixtures, it’ll look like this:

    nutrition_facts:
      id: 6
      name: Dish 6
      category: Cat 1
      description: Something about Dish 6
      price: 15.32
      menu_id: 1
      nutrition_facts: <%= {:serving_size => nil, :calories => 33}.to_yaml.inspect %>
    

    With my unit test now looking like this:

      test "no serving size with calories" do
        dish = dishes(:nutrition_facts)
    
        assert_not_nil(dish.calories)
        assert_nil(dish.serving_size)
        assert !dish.save, "Saved dish when serving size wasn't defined while calories was"
      end
    

    Source: Fixtures and serialized attributes in Ruby on Rails

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

Sidebar

Related Questions

I have table with column Percentage varchar(10) Data in that table is Pecentage 2/10
I have a table with a phone column, where data may have spaces, dots,dashes
I have table of data that is sorted as follows: Item | Sample |
I have two tables : DISH and DISH_HAS_DISHES. Dish table has all the dishes
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table in an MS Access database that looks like this: ID Symbol
I have a PostgreSQL 8.4 database that is created with the da_DK.utf8 locale. dbname=>
I have table named A with column B defined int not null Primary Key
I have table with around 70 000 rows. There is 6000 rows that i
I have table and this table contain result column with some entries. I just

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.