Subscribe
Contact



AddThis Social Bookmark Button

Judging by the number of Ruby on Rails developpers asking themselves this question,
this is the missing example of Rails. (It's not in my Rails reference book, and I've never seen any example on any blog.)

One solution is to create a new model for the association. It schould be the case
if you add attributes to the association (because push_with_attributes is now deprecated).
You can then simply find the association given the ids of your linked object and call destroy.

However, when you don't have any attribute in your liaison, the has_and_belongs_to_many
is nicer to work with. (you don't need a rails model for the liaison.)
Here is a link to the methods has_and_belongs_to_many adds where we can read :


"collection.delete(object, …) - removes one or more objects from the 
collection by removing their associations from the join table.
This does not destroy the objects."

Let's assume we dispose of 2 models 'Post' and 'Category' with a N-N association :

class Post < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

To delete an association (remove a post from a category) you can use this method :

  def remove_post_from_category
     post = Post.find(params[:post][:id])
     category = post.categories.find(params[:category][:id])

     if category
        post.categories.delete(category)
     end

  end

This function will destroy the association but won't destroy the category.

You can also removes all the categories from the posts by using :


collection.clear - removes every object from the collection. 
This does not destroy the objects.

In our example :

 
post.categories.clear

Every time your browser loads an new url, it also request a file called 'favicon.ico' in the same subdirectory. This generates a LOT of errors in the Ruby on Rails logs of my app.

One way to avoid those errors is to redirect the browser to the root /favicon.ico file.
Just add this line in the /public/.htaccess file of your rails project :

 RewriteRule ^(.*)favicon.ico$ favicon.ico [QSA]

Do you use another method to resolve this problem ?

has_and_belongs_to_many or has_many :through?
As you may (or may not) know, there are two ways to build a many-to-many (or N-N) relationship with Rails.
  1. The first way uses has_and_belongs_to_many in both models. You'll have to create a join table that has no corresponding model or primary key. ( ActiveRecord will look by default for a join table called groups_users )
  2. class Group < ActiveRecord::Base has_and_belongs_to_many :users # foreign keys in the join table end class User < ActiveRecord::Base has_and_belongs_to_many :groups # foreign keys in the join table end The join table can have more attributes which can be filled with the push_with_attributes method: class User < ActiveRecord::Base has_and_belongs_to_many :groups # foreign keys in the join table def join_group(group) groups.push_with_attributes(group, :joined_at => Time.now) end end
  3. The second way uses a has_many association with the :through option and a join model: class Subscriptions < ActiveRecord::Base belongs_to :group # foreign key - programmer_id belongs_to :user # foreign key - project_id end class Group < ActiveRecord::Base has_many :subscriptions has_many :users, :through => :subscriptions end class User < ActiveRecord::Base has_many :subscriptions has_many :groups, :through => :subscriptions end
If you have to work with the relationship model as its own entity, thent you'll need to use has_many :through. Otherwise, you can just stick to has_and_belongs_to_many.
Creating Rails Plugins
Just a link to "The Complete Guide to Rails Plugins" which is a reference.
Visualize your models
The "Visualize Models" plugin By Nils Franzen will generate .png images from the Rails model files : (click to enlarge) :

This plugin depends on GraphViz, which you can find here. From your rails application root, run :
ruby script/plugin install
svn://rubyforge.org//var/svn/visualizemodels/visualize_models
rake visualize_models
You'll then find your .png images in the "doc" directory.
Autocompletion with Rails and Scriptaculous
Ruby On Rails

Scriptaculous offers a nice autocompletion component that integrates quite well with Ruby on Rails.Here is a link that shows different ways to plug it in your rails application:

http://www.slash7.com/articles/2005/08/13/ajaxariffic-au...
Tutorial Ruby on Rails (en français)
French speakers only ;) Un petit cadeau d'Olivier Gutknecht à tous les français souhaitant découvrir Ruby on Rails :
Model validation with Rails
Ruby On Rails Rails has very nice features to validate a model (directly mapped on your database). However it gets sometimes sloppy if you don't know the exact command to do your validation.Here's a trick I had a hard time to find.
Everybody knows how to validate the uniqueness of a field in a database :
validates_uniqueness_of :date
If you now want to validate the uniqueness of a couple (ex: date,user_id) here's how you can:
validates_uniqueness_of :date, :scope => :user_id
Ruby on Rails with Apache2 and FastCGI
apt-get install ruby
wget http://rubyforge.org/frs/download.php/3463/rubygems-0.8.8.tgz
tar vxzf rubygems-0.8.8.tgz
cd rubygems-0.8.8
ruby setup.rb
gem update
gem install rails
© 2007 Eric Abouaf