# How to use two attributes in one validates\_inclusion\_of stat

**URL:** <https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756>\
**Category:** Rails\
**Created:** [January 29, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756 "2007-01-29T15:45:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756/1 "2007-01-29T15:45:32Z")

</div>

Hey hey 🙂

Might be a dumb-ass question, but then please bear with me.

I’m trying to add these two validations to a model:

validates\_inclusion\_of(:controller, :in =\> controllers, :message =\>  
‘does not exist in this application’)  
validates\_inclusion\_of(:action, :in =\>  
controllers[:action], :message =\> ‘is not implemented in the specified  
controller’)

Basically, controllers is a method that creates a hash of all  
controllers in my application, and an array of method names for each  
hash entry. The method works fine, and queries work like I would  
expect, e.g.,  
c = controllers =\> {“store”=\>[“index”, “list”, “search”,  
“show”], "a…  
c.include? ‘store’ =\> true  
c[‘store’].include? ‘index’ =\> true

The first validates\_inclusion\_of works fine. But I don’t know how to  
get the second validates\_inclusion\_of to use the value of the action  
attribute on the object in question. I’ve tried :action, action,  
@action, and @@action, but none of them contain the value of the  
action attribute on the object that I’m working on. How can I get to  
that attribute and use it in my validation statement above?

Thank you very much for any help 🙂

Daniel

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756/2 "2007-01-29T16:01:37Z")

</div>

Jason R. wrote:

> Could try params[:action] though I’m not sure if that’s available to you at  
> that time.
> 
> Jason

Not really, as it’s on the model level. Thanks, though 🙂

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756/3 "2007-01-29T15:50:33Z")

</div>

Could try params[:action] though I’m not sure if that’s available to you  
at  
that time.

Jason

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756/4 "2007-01-29T16:47:51Z")

</div>

self.bonk!

Yeah, missed that. This might just need to be controller-side  
validation. In  
terms of MVC architecture, the model should know nothing about the state  
of  
the controller.

Jason

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/how-to-use-two-attributes-in-one-validates-inclusion-of-stat/83756/5 "2007-01-29T17:00:53Z")

</div>

Jason R. wrote:

> self.bonk!

LOL 🙂

> Yeah, missed that. This might just need to be controller-side validation. In  
> terms of MVC architecture, the model should know nothing about the state of  
> the controller.

Yes, but it is not in the controller - both attributes are on the  
model. I want to check that the :controller value indeed matches an  
existing controller, and then, that the :action matches an action  
implemented in this controller. But to do the latter (the former works  
fine right now), I need to check my self-created controller hash at  
the correct index - like this,

validates\_inclusion\_of(:action, :in =\>  
controllers[:controller], :message =\> ‘is not implemented in the  
specified controller’)

Except that I don’t know how to get the value in the :controller  
attribute. Nothing I can think of writing yields that value… Do I  
make any sense? Here’s the entire model:

# == Schema Information

# Schema version: 6

# 

# Table name: navigation\_points

# 

# id :integer(11) not null, primary key

# parent\_id :integer(11)

# controller :string(255) default(), not null

# action :string(255) default(index), not null

# name\_da :string(255) default(), not null

# name\_en :string(255) default(), not null

# 

class NavigationPoint \< ActiveRecord::Base

acts\_as\_tree :order =\> ‘action’, :counter\_cache =\> :children\_count

validates\_presence\_of :controller,  
:action,  
:name\_da,  
:name\_en

validates\_inclusion\_of(:controller, :in =\> controllers, :message =\>  
‘does not exist in this application’)  
validates\_inclusion\_of(:action, :in =\>  
controllers[:controller], :message =\> ‘is not implemented in the  
specified controller’)

end

And here’s my ‘controllers’ method, which is a library function:

# Generates a 2D array of all possible controllers and their methods

in the application.  
def controllers

# Suck in all controllers:

require ‘find’  
Find.find(File.join(RAILS\_ROOT, ‘app/controllers’)) { |name|  
require\_dependency(name) if /\_controller.rb$/ =~ name  
}

# Consume all classes in a hash:

@classes = {}  
ObjectSpace.subclasses\_of(ActionController::Base).each do |obj|  
@classes["#{obj.controller\_name}"] = obj  
end

# Iterate over the hash, and create our controllers hash:

controllers = Hash.new()  
@classes.each\_value { |c|  
# Add the controller name:  
controllers["#{c.controller\_name}"] = []  
# Add all methods under the controller:  
@methods = c.public\_instance\_methods(false) # This one gets all  
methods, even hidden ones.  
@methods.sort.each { |m|  
controllers["#{c.controller\_name}"] \<\< m.to\_s  
}  
}

# Return the array

controllers  
end

Cheers,  
Daniel 🙂
