Reference is nill


class Restaurant < ActiveRecord::Base   self.table_name = "restaurants"   self.primary_key = "idrestaurant"   #SELECT `comidas`.* FROM `comidas` WHERE (`comidas`.restaurant_id = 2)   has_many :comidas, :foreign_key =>"idrestaurant" ,:primary_key=>'idrestaurant'   has_many :comentarios ,:foreign_key =>"idrestaurant" end

class Comentario < ActiveRecord::Base     self.table_name = "comentarios"   self.primary_key = "idcomentarios"   belongs_to :restaurant end controller_comentarios

def index     @comentarios = Comentario.all

    respond_to do |format|       format.html # index.html.erb       format.xml { render :xml => @comentarios }     end   end view comentarios index.html.erb

<h1>Listing comentarios</h1>

<table>   <tr>     <th>Restaurant</th>     <th></th>     <th></th>     <th></th>   </tr>

<% @comentarios.each do |comentario| %>   <tr> <td><%= comentario.comentario_%></td>     <td><%= comentario.estrellas %></td>       <td><%= comentario.usuario %></td>       <td><%= %></td>       <td><%= comentario.idrestaurant %></td>       <td> <%= Comentario.reflections.keys %> </td>       <td> <%= %> </td> <=its says true for all, so the restaurant is nill but comentario.idresaturant show the correct value

    <td><%= link_to 'Show', comentario %></td>     <td><%= link_to 'Edit', edit_comentario_path(comentario) %></td>     <td><%= link_to 'Destroy', comentario, :confirm => 'Are you sure?', :method => :delete %></td>   </tr> <% end %> </table>

<br />

<%= link_to 'New Comentario', new_comentario_path %> here is the code well formated. the question is that is nill, but its ok comentario.idrestaurant cause it has the right value. what im doing wrong? its suppose if i make @comentarios = Comentario.all, each element in @comentarios has to be a instance of restaurant not?


class Restaurant < ActiveRecord::Base self.table_name = "restaurants" self.primary_key = "idrestaurant" #SELECT `comidas`.* FROM `comidas` WHERE (`comidas`.restaurant_id = 2) has_many :comidas, :foreign_key =>"idrestaurant" ,:primary_key=>'idrestaurant' has_many :comentarios ,:foreign_key =>"idrestaurant" end

class Comentario < ActiveRecord::Base self.table_name = "comentarios" self.primary_key = "idcomentarios" belongs_to :restaurant

You have to specify the foreign_key here if it is not restaurant_id.

I hope you are using non-standard keys because you are dealing with a legacy database that you cannot change. If it is a new app your life will be much easier if you stick to the rails conventions. Similarly if it is a legacy db and you can change the schema then do it.