What I'm doing wrong, has_and_belong_to_many

Hello, I'm trying to insert into my migrate but when I try on rails
console this error shows:

1.9.3-p0 :001 > group = Group.new
=> #<Group id: nil, name: nil, description: nil, created_at: nil,
updated_at: nil>
1.9.3-p0 :002 > group.name = "Group Name"
=> "Group Name"
1.9.3-p0 :003 > group.description = "Description"
=> "Description"
1.9.3-p0 :004 > group.save
   (0.2ms) BEGIN
  SQL (0.4ms) INSERT INTO `groups` (`created_at`, `description`,
`name`, `updated_at`) VALUES ('2012-02-14 01:39:22', 'Description',
'Group Name', '2012-02-14 01:39:22')
   (124.5ms) COMMIT
=> true
1.9.3-p0 :005 > email = Email.new
=> #<Email id: nil, name: nil, email: nil, sex: "M", created_at: nil,
updated_at: nil>
1.9.3-p0 :006 > email.name = "Gremio10"
=> "Gremio10"
1.9.3-p0 :007 > email.email = "gremio@10.com"
=> "gremio@10.com"
1.9.3-p0 :008 > email.save
   (0.2ms) BEGIN
  SQL (0.5ms) INSERT INTO `emails` (`created_at`, `email`, `name`,
`sex`, `updated_at`) VALUES ('2012-02-14 01:39:56', 'gremio@10.com',
'Gremio10', 'M', '2012-02-14 01:39:56')
   (65.6ms) COMMIT
=> true
1.9.3-p0 :009 > group << email
NoMethodError: undefined method `<<' for #<Group:0xa7efb80>
  from
/home/felipe/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.1/lib/active_model/attribute_methods.rb:407:in
`method_missing'
  from
/home/felipe/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/attribute_methods.rb:126:in
`method_missing'
  from (irb):9
  from
/home/felipe/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in
`start'
  from
/home/felipe/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in
`start'
  from
/home/felipe/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:41:in
`<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'
1.9.3-p0 :010 >

My EmailModel

class Email < ActiveRecord::Base
  has_and_belongs_to_many :groups
  validate :email
end

My GroupModel

class Group < ActiveRecord::Base
  has_and_belongs_to_many :emails
  validate :name
end

My GroupsMigrate

class CreateGroups < ActiveRecord::Migration
  def change
    create_table :groups do |t|
      t.string "name"
      t.string "description"
      t.timestamps
    end
  end

  def down
    drop_table :groups
  end
end

My EmailsMigrate

class CreateEmails < ActiveRecord::Migration
  def change
    create_table :emails do |t|
      t.string "name"
      t.string "email"
      t.string "sex", :default => "M", :limit => 1
      t.timestamps
    end
  end

  def down
    drop_table :emails
  end
end

My EmailsGroupsMigrate

class CreateEmailsGroupsJoin < ActiveRecord::Migration
  def up
    create_table :emails_groups, :id => false do |t|
      t.references :email
      t.references :group
    end

    add_index :emails_groups, [ "email_id", "group_id" ]
  end

  def down
    drop_table :emails_groups
  end
end

Thank you.

Hello, I'm trying to insert into my migrate but when I try on rails
console this error shows:

...

1.9.3-p0 :009 > group << email
NoMethodError: undefined method `<<' for #<Group:0xa7efb80>

This is the chunk you need to worry about. It's telling you that you
haven't defined any << method for your Group model. AR doesn't
provide one, as such a thing just wouldn't make sense for most models.

class Email < ActiveRecord::Base
has_and_belongs_to_many :groups
validate :email
end

...

class Group < ActiveRecord::Base
has_and_belongs_to_many :emails
validate :name
end

You may also want to consider using "has_many :through" rather than
has_and_belongs_to_many. I've heard horror stories of people having
trouble getting HABTM to work right (though I never had problems with
it myself), and if you ever decide you want to "decorate" that
relationship with any additional data, it can be very difficult to
retrofit a HABTM relationship to become HMT.

-Dave

If I don't have the method '<<', how can I install on my rails ?

If you have a has_many relationship or if you have any collection, you will be able to use the append method << .

att

My error:

I was doing this -> group << email

But the correct is doing this -> group.emails << email

Thank you