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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:11:26+00:00 2026-05-26T08:11:26+00:00

I am a beginner at Ruby on Rails. Currently, I have the following problem:

  • 0

I am a beginner at Ruby on Rails. Currently, I have the following problem: I have a class Game that has an array of pictures and sentences alternating. I want that a user who creates a new Game is required to give one starting picture OR sentence. If he doesn’t do so I’d like to not save the newly created game to the data base.

class Game < ActiveRecord::Base
  has_many :sentences
  has_many :paintings

  validates_inclusion_of :starts_with_sentence, :in => [true, false]

  [...]
end

My approach was that on /games/new, the user has to give either one painting or one sentence to begin with, but I am unsure how to enforce this, especially how to create and save a child object along with the parent object in one step.

  • 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-26T08:11:26+00:00Added an answer on May 26, 2026 at 8:11 am

    So you’ve got two questions. The first (though second in your question) is “how to create and save a child object along with the parent object in one step.” This is a common pattern and looks something like this:

    class Game < ActiveRecord::Base
      has_many :sentences
      has_many :paintings
    
      accepts_nested_attributes_for :sentences, :paintings # <-- the magic
    end
    

    Then in, say, views/games/new.html.erb you can have something like this:

    <%= form_for :game do |f| %>
      <%= label :name, "Name your game!" %>
      <%= text_field :name %>
    
      <%= fields_for :sentence do |s| %>
        <%= label :text, "Write a sentence!" %>
        <%= text_field :text %>
      <% end %>
    
      <%= fields_for :painting do |s| %>
        <%= label :title, "Name a painting!" %>
        <%= text_field :title %>
      <% end %>
    <% end %>
    

    When this form is submitted Rails will interpret the POST parameters and you’ll end up with a params object that looks something like this:

    # params ==
    { :game => {
        :name     => "Hollywood Squares",
        :sentence => {
          :text => "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo."
        },
        :painting => {
          :title => "Les Demoiselles d'Avignon"
        }
      }
    }
    

    And finally, in the controller that receives those params:

    def create
      new_game = Game.create params[:game] # magic! the associated Sentence and/or
    end                                    # Painting will be automatically created
    

    That’s a very very high-level peek at what you’ll be doing. Nested attributes have their very own section in the docs.

    Your other question is how to enforce this. To do that you’ll need to write some custom validations. There’s two ways to do this. The simplest way is with validate, e.g.:

    class Game < ActiveRecord::Base
      # ...
    
      validate :has_sentence_or_painting  # the name of a method we'll define below
    
      private # <-- not required, but conventional
    
      def has_sentence_or_painting
        unless self.sentences.exists? || self.paintings.exists?
          # since it's not an error on a single field we add an error to :base
          self.errors.add :base, "Must have a Sentence or Painting!"
    
          # (of course you could be much more specific in your handling)
        end
      end
    end
    

    The other method is creating a custom validator class which lives in another file. This is particularly useful if you need to do a lot of custom validations or if you want to use the same custom validations on several classes. This, along with the single method, er, method, are both covered in the Validations Rails Guide.

    Hope that’s helpful!

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

Sidebar

Related Questions

I am a beginner Ruby on Rails developer. So far I have created several
I have a Ruby on Rails online marketplace that is broken up into categories.
I'm a beginner to ruby on rails that's migrating an access database to RoR.
I am a beginner with Ruby on Rails. I have a question regarding organizing
I am a beginner to ruby on rails.But i am quite comfortable with php,
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Absolute beginner question: I have a template file index.html that looks like this: ...
I'm still a beginner with ruby and rails, and now I'm googling about methods
I'm a beginner at Ruby On Rails and am trying to get a migration
I'm a beginner of Ruby and Rails, so this is probably an easy question.

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.