How to create a view to manage associations between HABTM models? (Rails)

Posted by Chris Hart on Stack Overflow See other posts from Stack Overflow or by Chris Hart
Published on 2010-05-06T00:48:11Z Indexed on 2010/05/06 4:08 UTC
Read the original article Hit count: 390

Hello, I am using Ruby on Rails and need to create a view that allows the creation of records through a HABTM relationship to another model. Specifically, I have the following models: Customer and ServiceOverride, and a join table customers_serviceoverrides. Using the customer view for create/update, I need to be able to create, update and delete ServiceOverrides and manage the attributes of the associated model(s) from the same view.

Visually I'd prefer to have something like a plus/minus sign to add/delete service overrides, and each serviceoverride record has two string entities which need to be displayed and editable as well. However, if I could just get the code (a kind of nested form, I'm assuming?) working, I could work out the UI aspects.

The models are pretty simple:

class ServiceOverride < ActiveRecord::Base
    has_and_belongs_to_many :customers
end

class Customer < ActiveRecord::Base
    has_and_belongs_to_many :serviceoverrides
end

The closest thing I've found explaining this online is on this blog but it doesn't really address what I'm trying to do (both manage the linkages to the other model, and edit attributes of that model.

Any help is appreciated. Thanks in advance.

Chris

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about associations