Forum: Ruby on Rails automatic update with has_and_belongs_to_many (:through)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-03-11 02:33
(Received via mailing list)

I have an application with a relation (many to many) via a :through. I
want this to be automatically updated just like with a regular
many_to_many. Just like I read here:

Is this possible? I just can't get it to work...

Here are my models:

class Declaration < ActiveRecord::Base
  #Foreign key is niet nodig, maar wordt wel aangeraden
  belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'
  has_many :declarations_payers
  has_many :users, :through => :declarations_payers

  validates_presence_of :user_id
  validates_numericality_of :user_id
  validates_presence_of :amount
  validates_numericality_of :amount
  validates_presence_of :description

  validates_associated :owner
  validates_associated :users


class DeclarationsPayer < ActiveRecord::Base
  belongs_to :user
  belongs_to :declaration

  validates_presence_of :declaration_id
  validates_numericality_of :declaration_id
  validates_presence_of :user_id
  validates_numericality_of :declaration_id

  validates_associated :user
  validates_associated :declaration

class User < ActiveRecord::Base
  has_many :deposits
  has_many :declarations_users
  has_many :declarations, :through => :declarations_users
  has_many :my_declarations, :class_name => 'Declarations', :through
=> :declarations_users

  def fullname
    return self.firstname + ' ' + self.lastname


Is it possible to add a new controller and automatically add the
appropriate users in the declarations_payers table?
Is was trying to do something like this:
  <%= check_box_tag 'declaration[declarations_payer.user]', %>

But it just didn't work...

Look here for my previous question about this application:

Thanks in advance!
This topic is locked and can not be replied to.