Topic: Problems using check_boxes

Hello!

Since this is my first post here and I'm fairly new with the whole RoR
concept, please bare with me (just wanted that to be out in the open)!
;D

I'm trying to make a checkbox for each input in another
model/database-table.

I have a database table called Roster which includes the colums "id" and
"name".

And then I have a table called matches that have colums "member1",
"member2" up to "member5". Those are all varchar's.

What I'm trying to do is to generate a checkbox for each member in the
Roster table into my matches controller.

In other words...


DATABASE's


Roster
---------------------
id | name            |
---------------------
1 | test1           |
2 | test2           |
3 | test3           |
4 | test4           |
5 | test5           |
. | ...             |
. | ...             |
58 | test58          |
---------------------


Matches
-------------------------------------------
member 1 | member 2 | ... | ... | member 5 |
-------------------------------------------
test1    | test 23  | ... | ... | test 38  |
-------------------------------------------


If you don't understand what I'm trying to achieve please don't hesitate
to ask, cause I will need  a somewhat "tutorial for dummies" to manage
this problem.

Thank you in beforehand

Christoffer Wallenius

Re: Problems using check_boxes

Before getting to the checkboxes, I would consider changing how the models are defined. Usually there is a better solution whenever you have numbers in the column names. Seeing 5 "member" columns is telling me there should be a "memberships" table. Perhaps this could have a match_id and roster_id column to join the matches and rosters tables. Or maybe you just want a simple HABTM association so you would create a matches_rosters table and store the joins through that.

If you can provide more details on how you want the relationship to be, I could give a better suggestion.

Railscasts - Free Ruby on Rails Screencasts

Re: Problems using check_boxes

The thing is that I want to store the members that attended on that certain match even if they are deleted from the members table. So therefore I where thinking that I would just plain out store the members names in the match table so that it won't go blank if a member get's kicked or leaves.

Basicly what I wanna do is to make a matchscript that can display the scores, what map, what members attended on that match, I would love for it to be flexible in a HABTM "system" but then I would need to add like a column in the members table like "deleted" so that if we kick a member their info would still be stored.

Re: Problems using check_boxes

Lockzi wrote:

I would love for it to be flexible in a HABTM "system" but then I would need to add like a column in the members table like "deleted" so that if we kick a member their info would still be stored.

That's what I would do. Creating an "active" type boolean column in the table feels like a cleaner design. This way all of their information is kept in case you need more than a name, and you aren't duplicating any information. This also fits more with the Rails convention of joining tables through their id rather than by a name of some sort.

Railscasts - Free Ruby on Rails Screencasts

Re: Problems using check_boxes

All right! I'll go with that... But would you mind explaining this for me?

If I print out all from my members table that have the column "active" as true.

[X]NameOfAMember_with_ID 51
[ ]NameOfAMember_with_ID 69
[X]NameOfAMember_with_ID 71
[X]NameOfAMember_with_ID 77
[ ]NameOfAMember_with_ID 81
[X]NameOfAMember_with_ID 86
[ ]NameOfAMember_with_ID 94
[X]NameOfAMember_with_ID 109

(Where [] is my way of telling you that it's an unchecked checkbox)

How would I be able to throw in the ID into the "member1-5" comlumn in the Match table? Practical example please if you don't mind.

I'm really unsure when it come's to hashes and checkboxes...

Thank you for your quick responses!


Christoffer Wallenius

Re: Problems using check_boxes

As I mentioned before, I recommend using a join table instead of 5 columns for storing members. It doesn't look like you will need any extra attributes in this join table, so a simple HABTM setup should work. To do this you would create a matches_rosters table like this:

create_table :matches_rosters, :id => false do |t|
  t.column :match_id, :integer
  t.column :roster_id, :integer
end

Then set up the associations in the models like this:

class Roster < ActiveRecord::Base
  has_and_belongs_to_many :matches
end

class Match < ActiveRecord::Base
  has_and_belongs_to_many :rosters
end


See this post for setting up checkboxes with a HABTM association. Just replace Tags and Expenses with Rosters and Matches respectively and things should work. If you need further clarification let me know.

Railscasts - Free Ruby on Rails Screencasts

Re: Problems using check_boxes

Thanks! I'll try it out and let you know how it works!...
Your help is very much appreciated.

Christoffer Wallenius