[code="ruby"]
class Group < ActiveRecord::Base
has_many :groups_users
has_many :users, :through => :groups_users
end
class User < ActiveRecord::Base
has_many :groups_users
has_many :groups , :through => :groups_users
end
[code="ruby"]
group.users.delete(user)
[/code]
Methods Added by has_many()
Just like belongs_to and has_one, has_many adds a number of attribute-related
methods to its host class. Again, these methods have names that start with
the name of the attribute. In the descriptions that follow, we’ll list the methods
added by the declaration
class Customer < ActiveRecord::Base
has_many :orders
end
orders(force_reload=false)
Returns an array of orders associated with this customer (which may be
empty if there is none). The result is cached, and the database will not be
queried again if orders had previously been fetched unless true is passed
as a parameter.
orders <
Adds order to the list of orders associated with this customer.
orders.push(order1, ...)
Adds one or more order objects to the list of orders associated with this
customer. concat is an alias for this method.
orders.replace(order1, ...)
Replaces the set of orders associated with this customer with the new
set. Detects the differences between the current set of children and the
new set, optimizing the database changes accordingly.
orders.delete(order1, ...)
Removes one or more order objects from the list of orders associated
with this customer. If the association is flagged as :dependent => :destroy
or :delete_all, each child is destroyed. Otherwise it sets their customer_id
foreign keys to null, breaking their association.
orders.delete_all
Invokes the association’s delete method on all the child rows.
orders.destroy_all
Invokes the association’s destroy method on all the child rows.
orders.clear
Disassociates all orders from this customer. Like delete, this breaks the
association but deletes the orders from the database only if they were
marked as :dependent.
orders.find(options...)
Issues a regular find call, but the results are constrained to return only
orders associated with this customer. Works with the id, the :all, and the
:first forms.
orders.count(options...)
Returns the count of children. If you specified custom finder or count
SQL, that SQL is used. Otherwise a standard Active Record count is
used, constrained to child rows with an appropriate foreign key. Any of
the optional arguments to count can be supplied.
orders.size
If you’ve already loaded the association (by accessing it), returns the size
of that collection. Otherwise returns a count by querying the database.
Unlike count, the size method honors any :limit option passed to has_many
and doesn’t use finder_sql.
orders.length
Forces the association to be reloaded and then returns its size.
orders.empty?
Equivalent to orders.size.zero?.
orders.sum(options...)
Equivalent to calling the regular Active Record sum method (documented
on page 315) on the rows in the association. Note that this works using
SQL functions on rows in the database and not by iterating over the
in-memory collection.
orders.uniq
Returns an array of the children with unique ids.
orders.build(attributes={})
Constructs a new order object, initialized using the given attributes and
linked to the customer. It is not saved.
orders.create(attributes={})
Constructs and saves a new order object, initialized using the given
attributes and linked to the customer.