Nick
Nick

Reputation: 2805

Is Property in any way reserved for rails model naming? (Rails pluralisation is killing me.)

I am trying to add a model called Properties in Rails 3.1 So I used created it using ryan bates nifty generator, although the model itself is very basic for now and only includes.

class Property < ActiveRecord::Base
  belongs_to :user
end

in my resources I have"

resources :properties

In one of my views I am simply trying to do the following:

<% for property in Property.all %>
 <p>description etc</p>
<% end %>

but it gives me the following error?!

undefined method `all' for Property:Module

Now it works if I replace Property.all with User.all or House.all but for some reason Property doesn't work. I'm kinda new to rails and think it has something to do with pluralization but I can't figure it out and its killing me. If anyone could please help that would be epic! Cheers

Upvotes: 0

Views: 324

Answers (2)

Anatoly
Anatoly

Reputation: 15530

You can use Inflections to extend default dictionary ( peoperty word is not included by default). The official API can help you with it

Upvotes: 1

creativetechnologist
creativetechnologist

Reputation: 1462

In your model definition you need to tell it the real name of your table, eg.

class Property < ActiveRecord::Base
    set_table_name "properties"
end

Also you may want to adapt the code slightly for showing your data, it's better to use your controller to grab the data, and the view to display it

In your controller

def index
  @properties = Property.all
end

In your view

<% @properties.each do |property| %>
  <%= property.description %>  
<% end %>

Upvotes: 0

Related Questions