Connor
Connor

Reputation: 640

Rails checkboxes for a has_many :through association

A person can compete in various events, but they must enter a partner's name for that event. This association is stored in an entry, which contains a field for the partner's name.

class Person < ActiveRecord::Base
    has_many :entries
    has_many :events, :through => :entries

    validates_presence_of :name
end

class Event < ActiveRecord::Base
end

class Entry < ActiveRecord::Base
    belongs_to :person
    belongs_to :event

    validates_presence_of :partner_name
end

The question is: How do you create a single page form that allows a person to enter themselves in multiple events and input their partners' names? I've tried to implement an all_entries method in the person model that will return an array of entry objects for all the available events, and an all_entries_attributes method that will update, create, and delete entry objects, but I can't seem to find a good, clean way to do this. I know this is a rather open ended question, but this must be a pattern that someone else in the rails community has encountered before, so I'm hoping there is a good solution to it.

Upvotes: 0

Views: 1344

Answers (2)

ok32
ok32

Reputation: 1321

If you are still looking for the answer you might want to check out my question and answer that I found.

Upvotes: 2

Andrei S
Andrei S

Reputation: 6516

So you want a page in which you can create events for a user, and add people to those events

I won't give you a plain solution because there's some work to an implementation for this, but I will recommend checking out these railscasts about nested model forms

part1 and part2

Upvotes: 0

Related Questions