Limiting the number of related records (has_many, but max number?)

89 views
Skip to first unread message

John Lahr

unread,
Jul 1, 2015, 11:51:54 AM7/1/15
to rubyonra...@googlegroups.com
Can you limit the number of related records a given parent record has?

Example.  Class/Student relatoinship.  A Class has_many students but I want to limit the size of a class to a max 30 (of course minimum or exact numbers would hopefully work the same way?)


Colin Law

unread,
Jul 1, 2015, 12:00:23 PM7/1/15
to rubyonra...@googlegroups.com
There may be a better way but I would just use a validation that
prevents further students being added to the class when the limit is
released.

By the way, can a student be in more than one class? If so then you
need has and belongs to many or (which I prefer) has many through.

Colin

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/aadce5fd-8e2a-4596-9b68-b88c952354a2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

John Lahr

unread,
Jul 1, 2015, 12:17:14 PM7/1/15
to rubyonra...@googlegroups.com
Colin

Thanks for your answer.  The student/class thing was just an example - the actual stuff I'd be building has nothing to do with students and classes it was just the easiest thing that came to me.

You can do a validation on a total number of related records?  I did not know that.

You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/IvBfZMAFC7g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Jul 1, 2015, 12:23:07 PM7/1/15
to rubyonra...@googlegroups.com
On 1 July 2015 at 17:16, John Lahr <john...@gmail.com> wrote:
> Colin
>
> Thanks for your answer. The student/class thing was just an example - the
> actual stuff I'd be building has nothing to do with students and classes it
> was just the easiest thing that came to me.
>
> You can do a validation on a total number of related records? I did not
> know that.

You can write a custom validation to do anything you like. The world
is your mollusc.

Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/CAEBjNa2e7-r8Uq0rrta9N9%2Bx0BROTXBJEq4UE9jgNWzv4H_g9Q%40mail.gmail.com.

John Lahr

unread,
Jul 1, 2015, 1:40:42 PM7/1/15
to rubyonra...@googlegroups.com
I did not know you could write custom validations.  Still learning

Hassan Schroeder

unread,
Jul 1, 2015, 1:55:36 PM7/1/15
to rubyonrails-talk
On Wed, Jul 1, 2015 at 10:40 AM, John Lahr <john...@gmail.com> wrote:
> I did not know you could write custom validations.

... which probably means you are ignoring an extremely valuable
learning resource: the Rails Guides.

e.g. http://guides.rubyonrails.org/active_record_validations.html

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

John Lahr

unread,
Jul 1, 2015, 2:08:12 PM7/1/15
to rubyonra...@googlegroups.com
Admittedly I haven't gotten through all of them as I'm still working through the basics...
Reply all
Reply to author
Forward
0 new messages