After a number of hours reading stackoverflow and watching railscasts, I’ve decided to post. The question is very similar if not identical to many other questions here but I’m just not getting it.
This is my first has_many_through association.
class User < ActiveRecord::Base
...
has_many :affiliations
has_many :sublocations, through: :affiliations
...
end
class Sublocation < ActiveRecord::Base
...
has_many :affiliations
has_many :users, through: :affiliations
...
end
class Affiliations < ActiveRecord::Base
...
belongs_to :user
belongs_to :sublocation
...
end
The affiliations table has the usual user_id and sublocation_id columns. It also has boolean column named ‘default’.
In my ‘new/edit user’ form(s), I’m needing to select one or more sublocations via checkboxes and also include a way to mark a sublocation as ‘default’.
Again, I’ve read example after example but something just isn’t ‘clicking’ in my brain. I’m not necessarily looking for an exact solution but a nudge in the right direction.
Many thanks,
CRS
My suggestion:
Create a user form to set up the associations between
UserandSublocationI am assuming the sublocations have been pre-populated?
users_controller.rb
users/_form.html.haml
Then perhaps on the user show page, list all their sublocations and create a form for them to set a default.
users/show.html.haml
routes.rb
Hope this helps or at least gets you on the right path.