delagoya (owner)

Revisions

gist: 198971 Download_button fork
public
Public Clone URL: git://gist.github.com/198971.git
Embed All Files: show embed
example_has_many_select.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# migration file
# assuming two models role.rb and recipe.rb
 
class CreateRecipesRoles < ActiveRecord::Migration
  def self.up
    create_table :recipes_roles, :id => false do |t|
      t.references :role
      t.references :recipe
    end
 
    add_index :recipes_roles, :role_id
    add_index :recipes_roles, :recipe_id
  end
 
  def self.down
    drop_table :recipes_roles
  end
end
 
 
# in the recipe model
has_many :roles
 
# in the roles mode
has_many :recipes
 
# in the views/roles/edit.html.erb
<%= select(:role, :recipe_id, Recipes.all.collect() {|r| [r.name, r.id]} , {}, {:multiple => true, :size => 5} ) %>