Reputation: 55
Hello I have a 1:M relationship between Customer_Status and Customer. Customer_Status applies to many Customers. I put the associations in the corresponding models.
Below is my schema
create_table "customer_statuses", force: :cascade do |t|
t.string "customer_status_desc"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "customers", force: :cascade do |t|
t.integer "customerstatus_id"
t.integer "customertype_id"
t.string "first_name"
t.string "last_name"
t.string "primaryemail"
t.string "secondaryemail"
t.string "billingcity"
t.string "billingstreet"
t.integer "billingzip"
t.integer "primaryphone"
t.integer "secondaryphone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
In my Customer Index View I am trying to display attribute customer_status_desc
from the Customer Status table instead of the customerstatus_id
found in the Customers table.
In the Customer Index View I have:
<% @customers.each do |customer| %>
<tr>
<td><%= customer.customer_status.customer_status_desc %></td>
<td><%= customer.customertype_id %></td>
<td><%= customer.first_name %></td>
<td><%= customer.last_name %></td>
</tr>
<% end %>
For the life of me I cannot get the customer_status_desc to display. I get the error undefined method customer_status_desc for nil:NilClass
I have tried naming it differently such as customer.customerstatus.customer_status_desc
After some research it appears the naming conventions are out of whack. Is there any way to resolve this without changing all of the names. Can I help rails understand what I am trying to call - or possibly force it through a query?
EDIT: I am using postgres and rails 4
Customers Controller
def index
@customers = Customer.order(sort_col + " " + sort_dir)
end
Upvotes: 1
Views: 68
Reputation: 101811
Class
and Module
names are CamelCase - CustomerStatus
never a unholy mix like Customer_Statuses
.
Other constants are ALL CAPS. This is the only convention the ruby interpreter cares about.
Everything else is snake_case. Attributes, method names, local variables. Snakecase is definiatly preferable to cramming the words together C style.
Do don't
primary_email primaryemail, primaryEmail
Again the Ruby interpreter does not care but your fellow programmers do. The Ruby ecosystem is actually wonderfully consistent. Don't be that guy.
In rails foreign keys by default are the relation name + _id
.
So if your model belongs_to :customer_status
. The column should be named customer_status_id
.
Active Record has some very specific conventions for how it maps class names to models.
Tables should be plural and models singular:
animals -> Animal
Compound words are somewhat special case as rails will treat the first word(s) as a namespace if it is plural.
user_hobbies -> UserHobby
users_hobbies -> Users::Hobbies
Join tables are somewhat special.
In has_and_belongs_to_many
both are plural:
hobbies_users
For has_many through:
the same rules as a normal table as rails actually loads a model:
hobby_users HobbyUser
Upvotes: 2
Reputation: 8026
Your column customerstatus_id
should be named customer_status_id
, and be sure that you have a belongs_to :customer_status
in your Customer
model.
Upvotes: 2