Ok ive got a workaround tha suits my situation.  The amount of has
many throughs
i have to cater for is not that great and i dont need to capture the
extra fields.

Some resources that may be usefull to others are here:

http://railsforum.com/viewtopic.php?id=803&p=1
http://railsforum.com/viewtopic.php?id=871&p=1

So my solution was to install the multiple select helper:
http://ruido-blanco.net/blog/rails-multiple-select-helper-plugin

products controller
def before_update_save(record)
      record.new_op_ids = params[:record][:operating_system_ids]
unless params[:record][:operating_system_ids].blank?
end

products helper
def operating_systems_form_column(record, input_name)
    collection_multiple_select(
    'record', 'operating_system_ids', OperatingSystem.find
(:all), :id, :name
    )
 end

product.rb model
has_many :product_operating_systems
has_many :operating_systems, :through => :product_operating_systems
attr_accessor :new_op_ids

after_save :update_operating_systems

def operating_system_ids
    self.operating_systems.map(&:id)
  end

  def update_operating_systems#=(new_ids)
    self.new_op_ids = (self.new_op_ids || []).reject(&:blank?)
    old_ids = self.operating_system_ids

    self.transaction do
      ProductOperatingSystem.destroy_all({:operating_system_id =>
old_ids - self.new_op_ids, :product_id => self.id })
      (self.new_op_ids - old_ids).each do |op_sys_id|
        self.product_operating_systems.create!(:operating_system_id =>
op_sys_id)
      end
    end
  end

any better solutions or refactoring would be appreciated, ideally we
want to override the form so it has a group of
product_operating_systems
so they are handled automatically by active_record but i couldnt get
this to work effeciently when i tried.

JB


On 23 May, 16:35, <[email protected]> wrote:
> > On 23 Apr, 09:03, johnnybutler7 <[email protected]> wrote:
> >> Hi,
>
> >> Hasanyone managed to gethasmanythroughrelationships to update and
> >> save?  I can get them to display on the forms but they never update or
> >> save.  My models\controllers are below.  So basically if i try to edit
> >> \create a product i get a check box list of the different operating
> >> systems Mac, PC, Linux etc that i can tick but they never save.  Ive
> >> tried some changes like changing operating_systems to
> >> product_operating_systems but it didnt work.  I know it will work with
> >> HABTM but i dont want to go down this route as its all setup to work
> >> withhasmanythrough.
>
> >> class Product < ActiveRecord::Base
> >>   has_many :product_operating_systems
> >>   has_many :operating_systems, :through=> :product_operating_systems
> >> end
>
> >> class OperatingSystem < ActiveRecord::Base
> >>   has_many :product_operating_systems
> >>   has_many :products, :through=> :product_operating_systems
> >> end
>
> >> class ProductOperatingSystem < ActiveRecord::Base
> >>   belongs_to :product
> >>   belongs_to :operating_system
> >> end
>
> >> class Admin::ProductsController < ApplicationController
> >>   active_scaffold :product do |config|
> >>     config.columns = [:operating_systems]
> >>    columns[:operating_systems].form_ui = :select
> >>   end
> >> end
>
> >> If anyone can help it would be appreciated as i have quite a
> >> fewhasmanythroughto use with AS.
>
> >> Im on rails 2.3.3 and the latest version of AS,
>
> >> JB
>
> When you use has_many :through, to create or edit you must use the has_many
> association. has_many :throughis used when relating model
> (ProductOperatingSystem in your case)hasother attributes, so you must
> create a ProductOperatingSystem to fill that attributes, you can't
> associate a Product with an Operating System directly.
>
> When create/edit a product, you should create ProductOperatingSystem lines
> to fill some attributes and select an OperatingSystem.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to