Reference is nill


class Restaurant < ActiveRecord::Base
self.table_name = “restaurants”
self.primary_key = “idrestaurant”
#SELECT comidas.* FROM comidas WHERE (comidas.restaurant_id =
has_many :comidas, :foreign_key
=>“idrestaurant” ,:primary_key=>‘idrestaurant’
has_many :comentarios ,:foreign_key =>“idrestaurant”

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

def index
@comentarios = Comentario.all

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

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 %>
<%= comentario.comentario_%> <%= comentario.estrellas %> <%= comentario.usuario %> <%= %> <%= comentario.idrestaurant %> <%= Comentario.reflections.keys %> <%= %>

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

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


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.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs