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 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

Related Questions

I have a Game model which has_many Rounds which has_many Shots. Per game, each
I have written a checkers game model which has a Board object that holds
So I have this model Game that has an Awayteam and Hometeam field. Now
In my model I have Games. Each Game has an Event associated with it,
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
In my controller I have model operations that can return empty results. I've setup
I have a Django model, which is essentially a list of words. It is
I have a Games model which hasMany Avatars and Agents. When i remove the

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.