Walter Davis wrote in post #1023809:
What does the relationship look like between Course and Customer? Is
there an Enrollment object sitting between them?
One way might be to have a form_for @course on your Customers#index
page, and a submission of that to the CoursesController would build a
new Enrollment for each submitted Customer on that Course.
Walter
Hi Walter,
Thank you for your response.
There is no direct releationship between customers and course tables.
Then in order to have such a relationship (or the inverse -- a lack of relationship) between any given Course and any given Customer, you will need to store that graph somewhere. I would make a new Enrollment model, and give it the following attributes:
customer_id
course_id
You could add other decorations to it if you like, certainly the default timestamps could be useful, but you could also add a column for how much the customer paid, any other details about this transaction you need to store and retrieve later.
Then, in the model file, you would add
belongs_to :course
belongs_to :customer
And in your Course and Customer models, you would add the same declaration in each:
has_many :enrollments
And the specific declaration for each:
has_many :courses, :through => :enrollment
(in Customer)
has_many :customers, :through => :enrollment
(in Course)
Could you please be more specific in regard to:
One way might be to have a form_for @course on your Customers#index
page, and a submission of that to the CoursesController would build a
new Enrollment for each submitted Customer on that Course.
I just want to display all customers for a selection (checkbox style).
The rest is pretty easy. If you have a form for a course, you can gather all of the possible attendees for that course using
@customers = Customer.all
in your CoursesController. The relationship will take care of everything else.
In your enrollment form view, you would then iterate over those, and build your checkboxes (I've left out all the html and erb tags for clarity):
form_for @course do |f|
@customers.each do |customer|
f.check_box [:customers, customer], @course.customers.include?(customer)
f.label customer.name, [:customers, customer]
end
f.submit
end
That's off the top of my head, but it should point you in a working direction.
Walter