<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: habtm code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 17:53:40 GMT</pubDate>
    <description>DZone Snippets: habtm code</description>
    <item>
      <title>Using a mutiple collection select form helper with habtm or has many associations in Rails</title>
      <link>http://snippets.dzone.com/posts/show/4369</link>
      <description>// where user has and belongs to many roles&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# in your view&lt;br /&gt;&lt;%= collection_select :user, :role_ids, Role.find(:all, :order =&gt; 'name ASC'), :id, :name, { :selected =&gt; @user.role_ids }, { :multiple =&gt; true, :name =&gt; 'user[role_ids][]' } -%&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 30 Jul 2007 08:58:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4369</guid>
      <author>davenolan (davenolan)</author>
    </item>
    <item>
      <title>HABTM relationship MySQL tables for use in Rails</title>
      <link>http://snippets.dzone.com/posts/show/3169</link>
      <description>This is an example of the tables I create when I want to have an HABTM relationship between 2 tables:&lt;br /&gt;   Table 1: products&lt;br /&gt;   Table 2: tags&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DROP TABLE IF EXISTS `products_tags`;&lt;br /&gt;DROP TABLE IF EXISTS `tags`;&lt;br /&gt;DROP TABLE IF EXISTS `products`;&lt;br /&gt;&lt;br /&gt;CREATE TABLE `products` (&lt;br /&gt;  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;  `title` varchar(100) NOT NULL,&lt;br /&gt;  `price` decimal(10,2) NOT NULL,&lt;br /&gt;  PRIMARY KEY  (`id`)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;CREATE TABLE `tags` (&lt;br /&gt;  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;  `title` varchar(64) NOT NULL,&lt;br /&gt;  PRIMARY KEY  (`id`)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;CREATE TABLE `products_tags` (&lt;br /&gt;  `product_id` int(11) NOT NULL default '0',&lt;br /&gt;  `tag_id` int(11) NOT NULL default '0',&lt;br /&gt;  PRIMARY KEY  (`product_id`,`tag_id`)&lt;br /&gt;)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 19 Dec 2006 15:14:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3169</guid>
      <author>alexwilliams (Alex Williams)</author>
    </item>
    <item>
      <title>Toggling associations for Many-to-Many relationships</title>
      <link>http://snippets.dzone.com/posts/show/2813</link>
      <description>I have a rather greedy object that needs to be associated with most of the other objects in my domain.  To compensate, I've written the following code to take care of toggling the association (adding a record in the join table if it does not exist or removing it if it does).  I've called it toggle because it's physically represented by toggling the state of a checkbox on a form.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def toggle_child_assoc(child_object)&lt;br /&gt;    # check to see if the parent recognizes the collection_name&lt;br /&gt;    collection_name = child_object.class.to_s.downcase.pluralize&lt;br /&gt;    aCollection = self.send(collection_name) if self.respond_to?(collection_name)&lt;br /&gt;    unless aCollection&lt;br /&gt;      return false&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # If the parent recognizes the collection_name then check to see if the child_object&lt;br /&gt;    # appears in the collection.  If so then remove it, otherwise add it.&lt;br /&gt;    if aCollection.include?(child_object)&lt;br /&gt;      self.send("remove_%s" % collection_name, child_object)&lt;br /&gt;    else&lt;br /&gt;      self.send("add_%s" % collection_name, child_object)&lt;br /&gt;    end&lt;br /&gt;    true&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;First the code determines if the "parent" of the association actually supports a collection of the child object's class by testing to see if it responds to the method corresponding to the name of the collection.  If not the method returns false.  Otherwise, the method checks to see if the parent object already has the child in its collection.  If it does it builds a call to remove_collection_name to remove it and if it does not it builds a call to add_collection_name to add it.&lt;br /&gt;&lt;br /&gt;The code could be improved by adding a means by which to override the collection_name.  This override would be used in the event that the child is a subclass of a class with which the parent has a habtm relationship.</description>
      <pubDate>Fri, 13 Oct 2006 21:24:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2813</guid>
      <author>AndyV (Andy Vanasse)</author>
    </item>
    <item>
      <title>Doing a has_and_belongs_to_many against a single table</title>
      <link>http://snippets.dzone.com/posts/show/284</link>
      <description>Found via the RoR wiki. Solution courtesy of Jakob S.&lt;br /&gt;&lt;br /&gt;Assuming a schema like such:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;create table node (&lt;br /&gt;  id integer primary key,&lt;br /&gt;  name varchar(32)&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;create table nodelink (&lt;br /&gt;  source_id integer,&lt;br /&gt;  destination_id integer&lt;br /&gt;);&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;A Rails association like this would supposedly do the job:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;has_and_belongs_to_many :nodes, :join_table =&gt; 'nodelink', :association_foreign_key =&gt; 'destination_id', :foreign_key =&gt; 'source_id'&lt;/code&gt;</description>
      <pubDate>Sat, 14 May 2005 09:44:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/284</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
