Overwrite methods and using has_many

Dear Community,
I'm using Ruby and especially Ruby on Rails only for a short time, so
please excuse me, if the answer to my following question is obvious.
Please also excuse my bad English, it's not my mother tongue - feel
free to give me hints how to improve my English :slight_smile:

I have a two model files called StorageSlot and Product:
class StorageSlot < ActiveRecord::Base
  has_many :products
  belongs_to :building
end

class Building < ActiveRecord::Base
  belongs_to :storage_slots
end

Now I can access all products belonging to a slot by using
slot.products. I even can add products using slot.products <<,
slot.products.build, slot.products.create, and so on. Each slot has a
specified number of maximal products, which is of course not
recognized by these methods. How can I overwrite them to perform a
check if the slot is already full ?

I hope you understand my problem as I don't have any ideas how to
solve it. Thank you for your help in advance and greetings from
Austria :slight_smile:

Christoph

class Building < ActiveRecord::Base
belongs_to :storage_slots
end

I assume you mean class Product.

Now I can access all products belonging to a slot by using
slot.products. I even can add products using slot.products <<,
slot.products.build, slot.products.create, and so on. Each slot has a
specified number of maximal products, which is of course not
recognized by these methods. How can I overwrite them to perform a
check if the slot is already full ?

Use a before_add callback ?

Fred

Yes, I'm sorry, I copied the false piece of code^^ :(.

I assume you mean class Product.

Yes, I'm very sorry, I copied the false piece of code^^ :(.

Use a before_add callback ?

Sorry, as I'm a newbie, could you please explain it to me.

Christoph

see association callbacks at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Fred

see association callbacks athttp://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet…

Thank you very much, this is exactly what I was looking for. I'm very
glad about the quick and friendly help and - once again - about the
capabilities of Rails.

Christoph