# Using singular class - SoftwareController

**URL:** <https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738>\
**Category:** Rails\
**Created:** [February 7, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738 "2007-02-07T13:16:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 7, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/1 "2007-02-07T13:16:30Z")

</div>

I’m having trouble getting a singular class name to work with rails.  
We have a table, software, that lists all the software. We want a  
SoftwareController, Software model, and then REST paths like  
new\_software\_path

I’ve tried using the Inflector in config/environment.rb, but neither  
of these works:

Inflector.inflections do |inflect|  
inflect.uncountable %w(software) # Nope  
inflect.irregular ‘software’, ‘software’ # Nope  
end

I can only get it to work by explicitly linking to :controller =\>  
‘software’, and even then it wants to force-load SoftwaresHelper from  
helpers/softwares\_helper.rb

Any hints?

-Nate

---

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 7, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/2 "2007-02-07T14:22:36Z")

</div>

Thinking out loud here:

Assuming that your model is defined like this:

class Software \< ActiveRecord::Base

set\_table\_name “software”

end

Then that will take care of the plural Softwares stuff… I think that  
the  
RESTFul routing might expect plural controllers… but rails 1.2.2  
(released  
yesterday) can help solve that problem - see singular resources in the  
following post:

[http://weblog.rubyonrails.com/2007/2/6/rails-1-2-2-sqlite3-gems-singular-resources](http://weblog.rubyonrails.com/2007/2/6/rails-1-2-2-sqlite3-gems-singular-resources)

---

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 7, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/3 "2007-02-07T15:49:51Z")

</div>

On Feb 7, 7:15 am, “Nate W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve tried using the Inflector in config/environment.rb, but neither  
> of these works:
> 
> Inflector.inflections do |inflect|  
> inflect.uncountable %w(software) # Nope  
> inflect.irregular ‘software’, ‘software’ # Nope  
> end

After the inflections, add this line:  
ActionController::Routing::Routes.reload

However, also look into Brian’s comment about the singular resources.

Dan M.

---

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 9, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/4 "2007-02-09T22:09:40Z")

</div>

A couple of thoughts on this:

Singular resources aren’t useful when you’re using an  
Inflector#uncountable object in a has\_many relationship. In my case,  
I’ve got Personnel. The singular and plural form of Personnel remains  
the same, so in config/environment.rb, I set up  
Inflector.inflections do |inflect|  
inflect.uncountable %w( personnel )  
end

then I ran my script/generate scaffold\_resource Personnel. After  
adding  
self.table\_name = ‘personnel’  
to the model, I still had to monkey the routes around.

In Rails 1.2.1, this works just fine:  
map.resources :personnel, :controller =\> ‘personnel’, :singular =\>  
‘personnel’

After a mongrel\_rails restart, everything was working as expected.

In Rails 1.2.2, this same code falls apart and fails. In my case, I  
don’t want to use a singular resource, because the following URL:  
[http://example.com/personnel](http://example.com/personnel)  
should give me the index action, the list of personnel available.  
With singular resource, that URL is mapped to the show action.

- Jared

---

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 9, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/5 "2007-02-09T22:44:52Z")

</div>

> then I ran my script/generate scaffold\_resource Personnel. After  
> In Rails 1.2.2, this same code falls apart and fails. In my case, I  
> don’t want to use a singular resource, because the following URL:  
> [http://example.com/personnel](http://example.com/personnel)  
> should give me the index action, the list of personnel available.  
> With singular resource, that URL is mapped to the show action.
> 
> - Jared

Singular resources are for resources that don’t have index actions.  
For instance, an app like Basecamp has some notion of Accounts. Yet,  
perhaps you want users to view and modify their own account from  
[Foo.com](http://rick.foo.com/account). /account maps to the show action and  
gets the id value from somewhere else. In this case, it would  
probably do something like Account.find\_by\_name(‘rick’). Other use  
cases include:

/settings  
/users/5/avatar

etc.

I have no clue if Basecamp does this, I just use that as an example…

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![nwiger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@nwiger](https://www.ruby-forum.com/u/nwiger)\
**Post date:** [February 9, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/using-singular-class-softwarecontroller/84738/6 "2007-02-09T23:35:57Z")

</div>

I’d been hoping to get a good explanation of Singular Resources,  
thanks Rick!

On another note, the following does fall apart on Rails 1.2.1:

map.resources :personnel, :controller =\> ‘personnel’, :singular =\>  
‘personnel’

Because the url\_for helper can’t distinguish between:  
personnel\_path - Plural, should map to Index  
personnel\_path(@personnel) - Singular should map to show.

If I’m looking at a ‘show’ page, such as [http://example.com/personnel/](http://example.com/personnel/)  
1, the following helper  
\<%= link\_to ‘Back’, personnel\_path %\> still generates  
[http://example.com/personnel/1](http://example.com/personnel/1),  
since it’s picking up the params[:id] value of the current page.

My workaround was to change the singular name, since the plural name  
looks singular:  
map.resources :personnel, :controller =\> ‘personnel’, :singular =\>  
‘person’

and use \<%= link\_to ‘Show’, person\_path(@personnel) %\> for all my  
singular-style link\_to helpers.
