# Overwrite methods and using has\_many

**URL:** <https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185>\
**Category:** Rails\
**Created:** [June 11, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185 "2008-06-11T18:16:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 11, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185/1 "2008-06-11T18:16:12Z")

</div>

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 🙂

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 🙂

Christoph

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 11, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185/2 "2008-06-11T18:18:34Z")

</div>

On Jun 11, 5:10 pm, Christoph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 11, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185/3 "2008-06-11T18:29:43Z")

</div>

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

On 11 Jun., 18:18, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 11, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185/4 "2008-06-11T19:07:33Z")

</div>

On 11 Jun 2008, at 17:29, Christoph wrote:

> > Use a before\_add callback ?  
> > Sorry, as I’m a newbie, could you please explain it to me.  
> > see association callbacks at  
> > [ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)

Fred

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 11, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/overwrite-methods-and-using-has-many/140185/5 "2008-06-11T20:05:06Z")

</div>

> 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
