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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:01:36+00:00 2026-05-13T13:01:36+00:00

I have a model, Game , which has_many :piles . In fact, I know

  • 0

I have a model, Game, which has_many :piles. In fact, I know that each Game has exactly 4 piles, each of which has a different (at the scope of the Game) contents. My web-form for creating a Game allows the user to pick the four contents (as c_type_#). I’m therefore able to populate the Piles at creation of the Game. However, I can’t figure out how to ensure that I have precisely 4 unique Piles. My models look like this:

class Game < ActiveRecord::Base
  has_many :piles

  def after_create
    1.upto(4) do |num|
      piles.create("contents" => "c_type_#{num}")
    end
  end
end

class Pile < ActiveRecord::Base
  belongs_to :game
  validates_uniqueness_of :contents, :scope => "game_id"
end

… and my migration to add Piles looks like:

class CreatePiles < ActiveRecord::Migration
  def self.up
    create_table :piles do |t|
      t.integer :game_id
      t.string :contents
    end

    add_index :piles, [:game_id, :contents], :unique => true
  end

  def self.down
    drop_table :piles
  end
end

…but all this means is that the non-unique Piles aren’t added to the database, silently; and the parent Game ends up with fewer than 4 piles.

I have currently worked around this by having Game validate :unique_pile_contents, :on => :create, where unique_pile_contents verifies that the length of a uniq’d array of the c_type_# values is 4 – but this feels very kludgy. Is there a better way?

  • 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-13T13:01:37+00:00Added an answer on May 13, 2026 at 1:01 pm

    I’ve managed to solve this as part of another problem I was tackling. See Creating a set number of child objects from a parent's form for the answer (and a slightly simpler example in the question).

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

Sidebar

Ask A Question

Stats

  • Questions 272k
  • Answers 272k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer BNF doesn't "care" for user names per-se. It works on… May 13, 2026 at 1:54 pm
  • Editorial Team
    Editorial Team added an answer I thought the answers on your previous question on this… May 13, 2026 at 1:54 pm
  • Editorial Team
    Editorial Team added an answer Is the BLOB initially in UTF-8 format ? If it's… May 13, 2026 at 1:54 pm

Related Questions

I'm trying to model a card game in order to learn Rails. This is
This follows this prior question, which was answered. I actually discovered I could remove
By 'view' here I mean different combinations of properties of the model, not the
In my model I have Games. Each Game has an Event associated with it,
I'm working on an RTS game in C++ targeted at handheld hardware (Pandora). For

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.