Reference is nill

models

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

Listing comentarios

<% @comentarios.each do |comentario| %>

<=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 %>

<% end %>
Restaurant
<%= comentario.comentario_%> <%= comentario.estrellas %> <%= comentario.usuario %> <%= comentario.restaurant %> <%= comentario.idrestaurant %> <%= Comentario.reflections.keys %> <%= comentario.restaurant.nill? %>

<%= link_to ‘New Comentario’, new_comentario_path %>
here is the code http://pastie.org/1524215 well formated. the question
is that comentario.restaurant 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?

On 3 February 2011 08:53, lorbrito [email protected] wrote:

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.

Colin