Question about has_and_belongs_to_many

howdy!
i have got two models: a photo model and a lightbox ( a group of photos)
model.

they look like this:

class Lightbox < ActiveRecord::Base
acts_as_taggable
has_and_belongs_to_many :photos
end

class Photo < ActiveRecord::Base
acts_as_taggable
validates_presence_of :filename
has_and_belongs_to_many :lightboxes
end

and my db migration looks like this:

class CreateLightboxes < ActiveRecord::Migration
def self.up
create_table “lightboxes” do |t|
t.column “description”, :text
t.column “name”, :string
t.column “created_at”, :date
end

create_table("lightboxes_photos", :id=>false) do |t|
  t.column "lightbox_id", :integer
  t.column "photo_id", :integer
end

end

def self.down
drop_table “lightboxes”
drop_table “lightboxes_photos”
end
end

how can i add a photo to a lightbox?
i you need more code to help me just tell me
thanks for your help
flo

how can i add a photo to a lightbox?

add a new photo

lightbox = Lightbox.find(1)
lightbox.photos.create(:filename => ‘me.jpg’)

add an existing photo

lightbox = Lightbox.find(1)
photo = Photo.find(1)
lightbox.photos.push(photo)
lightbox.save

Hope this helps.

Cheers,
Mischa


http://boxroom.rubyforge.org