Reputation: 1180
I am setting up a situation where I only want certain scopes of my model to "has_many" of another model. As a hypothetical situation, imagine I have a model called Tree
and a model called Apple
. I only want tall trees to be allowed to have apples, so I scoped my tree model into tall
. How can I use a has_many appropriately in this situation?
I am using Rails 4.
Sample Code
Tree.rb
scope :tall, where(:tall => true)
scope :short, where(:short => true)
has_many :apples (need to alter this line to only have this happen for :scope tall)
Apple.rb
belongs_to :tall, class_name: 'Tree'
I am unsure if the rest of the code is the right way either, so please let me know if there are any improvements I can make to it.
Upvotes: 0
Views: 120
Reputation: 1340
The gem 'pundit' makes it easy to create and allow model specific and oo procedures. May be what you are looking for.
https://github.com/elabs/pundit
Upvotes: 2