Simple and elegant checkboxes with has_many through impossible?


Im currently developing simple website to manage some sort of product.
Every product has its own product_categories, so these are related
with has_many :through association. product has many product
categories. im not using habtm, i prefer has_many through and i can’t
change it, so habtm is out of scope. quite simple isn’t it ?

Ok, so i tried to make a CRUD for it, you know simple thing like:
name for a product, and list of checkboxes for product categories.
when i check some of checboxes, the categories are assigned to my
product, at least one category checkbox has to be checked
(validation), nothing special.

I came up with this (based on some articles from this forum):

The whole project is available to download and run on google code:

Feel free to post code patches through issue tracker if you want to.

The code above comes from this forum and i feel it’s rails hacking a
little bit, and it doesn’t work as it should, here’s why:

Here are my really serious problems:

  1. Checkboxes dont remember their state when validation fails. For
    example, when i check two of them, and hit submit (leaving name
    textfield empty which causes validation to fail) i get the view again,
    but checkboxes i checked are reset to the initial state. really

  2. My unit tests for Product model does not pass. Why ? Because im
    doing validation through custom validate method, which checks
    product_category_ids attribute (this is attribute generated by rails
    automaticaly right ? ) which is simply view related, not model. so
    when i create in my test Product and assign category to it, it fails
    because im assigning product_category to my product, not

Question is simple, how do i do has_many with checkboxes so that works
like any other simple field of my model like name where it remember
state between postbacks and does all validations i want
(validates_presence_of) ?

Best regards

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs