limit to has_many ?

42 views
Skip to first unread message

CharlieMagee

unread,
Oct 3, 2011, 6:42:34 PM10/3/11
to Hobo Users
Hi,

I've got a model for bowling teams, a model for bowling games, and a
model for game assignments. My has_many and belongs_to setup is
working just fine.

But I've got 16 teams, and when I create a new game I want to limit
the team picking to 2. Right now, I pick two teams, but the button to
"Add Bowling Team" pops up again and I can add another and another and
so on.

How do I limit that field to two, while keeping the Remove button
still next to the first two choices so I can edit those if I make a
mistake.

Thanks,

Charlie Magee

kevinpfromnm

unread,
Oct 3, 2011, 10:08:17 PM10/3/11
to hobo...@googlegroups.com
I'd probably use a separate belongs_to association for each of the two teams.  You don't really want a many-many relationship as teams can have many games but each game can have only two.  The downside is it becomes a little bit trickier to handle any sql to find what games a team was in since there are two relationships.

I'm not sure if there's a way to limit that without digging deep into the input-many tag.  Maybe just add a validation to ensure there's 2 and only 2 teams selected?

Wait, a quick glance at the tag and you might be able to get what you want by something like <input-many minimum="2" without-add-item without-remove-item /> Untested.

CharlieMagee

unread,
Oct 4, 2011, 6:02:28 PM10/4/11
to Hobo Users
Hi Kevin,

I'm trying out your suggestion, but I'm sure the problem I'm having is
where to put that line of code. I'm assuming in the form for that
model.

Here's the form before trying your suggestion:
<def tag="form" for="NflGame">
<form merge param="default">
<error-messages param/>
<field-list fields="name, winner, nfl_cals, nfl_teams" param/>
<div param="actions">
<submit label="#{ht
'nfl_game.actions.save', :default=>['Save']}" param/><or-cancel
param="cancel"/>
</div>
</form>
</def>


My first try, I stuck it here:
...
<error-messages param/>
<field-list fields="name, winner, nfl_cals, nfl_teams" param/>
<input-many minimum="2" without-add-item without-remove-item />
<div param="actions">
...

That got an undefined method error.

My second try didn't produce an error, but it also didn't change
anything:

<field-list fields="name, winner, nfl_cals, nfl_teams" input-many
minimum="2" without-add-item without-remove-item param/>

Sorry for being such a noob. I'm floundering in the early stages of
wrapping my mind around how all the pieces fit together. Once I get to
that point my questions will be less stupid. But for now, I'm not even
sure where to look in the PDF doc.

Thanks again,

Charlie

CharlieMagee

unread,
Oct 4, 2011, 6:04:21 PM10/4/11
to Hobo Users
And if I use a belongs_to for each two teams, then I have to do that
for all the combinations of any team playing any other team? That
doesn't sound right?

C

kevinpfromnm

unread,
Oct 4, 2011, 6:53:22 PM10/4/11
to hobo...@googlegroups.com
The input-many should replace the field in the field-list

<field-list ...>
  <nfl-teams-view:><input-many minimum... /></nfl-teams-view:>
</field-list>

Though, considering that you're basically using the default form you could switch to just extending the form to make your changes (I post this mostly to show how to make for dry use of existing forms/cards/etc)

<extend tag="form" for="NflGame">
  <old-form merge>
    <field-list:>
      ... above customization of field list
    </field-list:>
  </old-form>
</extend>

Given how little code was in yours, not a huge deal to redo it, but for other tags it can be a huge savings.  Also, nice thing with this flow is it should respond automatically to changes to the model other than the one piece of custom code.

CharlieMagee

unread,
Oct 5, 2011, 5:34:30 PM10/5/11
to Hobo Users
Thanks for the new tips Kevin. I'll have to try them later. Right now
I'm wrestling with why my projects won't run in Heroku. Very
frustrating. They run fine in localhost, and the tutorial apps ran
fine in Heroku, but now I just keep getting that standard Heroku
"something's wrong" error. I know I'm doing something stupid.

But it's getting the push fine and the heroku rake db:migrate runs
without error, but the project won't play when I try to open it.
Sheesh.

Charlie

kevinpfromnm

unread,
Oct 5, 2011, 6:23:49 PM10/5/11
to hobo...@googlegroups.com
heroku logs
Reply all
Reply to author
Forward
0 new messages