开发者

rails 3, undefined method `eq' for nil:NilClass

i'm create scaffold pucgrupo

rails g scaffold pucgrupo numero:integer clase_id:integer nombre:string

after edit my migration

class CreatePucgrupos < ActiveRecord::Migration
  def self.up
    create_table :pucgrupos , :id => false do |t|
      t.integer :numero
      t.integer :clase_id
      t.string :nombre

      t.timestamps
    end
  end

  def self.down
    drop_table :pucgrupos
  end
end

and eject the migration

rake db:migrate

after when I try to create a group I get this error:

NoMethodError in PucgruposController#show

undefined method `eq' for nil:NilClass
Rails.root: /home/andres/desarrollos/rubyonrails/proyecto

Application Trace | Framework Trace | Full Trace
app/controllers/pucgrupos_controller.rb:16:in `show'
Request

Parameters:

{"id"=>"0"}

i think that is for column does not exist "id" any ideas ?

1 edit

i'm edit migration primary_key numero:

class CreatePucgrupos < ActiveRecord::Migration
  def self.up
    create_table :pucgrupos, :id => false , :primary_key => :numero do |t|
      t.integer :numero
      t.integer :clase_id
      t.string :nombre
      t.timestamps
    end
  end

  def self.down
    drop_table :pucgrupos
  end
end

the data insert into table, but get the same error.

2 edit

edit migration:

create_table :pucgrupos, :primary_key => :numero do |t|
  t.integer :numero
  t.integer :clase_id
  t.string :nombre
  t.timestamps
end

the error display page is:

NoMethodError in PucgruposController#show

undefined method `eq' for 开发者_运维问答nil:NilClass
Rails.root: /home/andres/desarrollos/rubyonrails/proyecto

Application Trace | Framework Trace | Full Trace
app/controllers/pucgrupos_controller.rb:16:in `show'
Request

Parameters:

{"id"=>"1"}

3 edit

i'm edit pucgrupos_controller.rb methos show is

  def show
    @pucgrupo = Pucgrupo.find_by_numero(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pucgrupo }
    end
  end

the error display page

ActionController::RoutingError in Pucgrupos#index

Showing /home/andres/desarrollos/rubyonrails/proyecto/app/views/pucgrupos/index.html.erb where line #18 raised:

No route matches {:controller=>"pucgrupos", :action=>"show", :id=>#<Pucgrupo numero: 1, clase_id: 2, nombre: "aaa", created_at: "2011-08-18 17:13:07", updated_at: "2011-08-18 17:13:07">}
Extracted source (around line #18):

15:     <td><%= pucgrupo.numero %></td>
16:     <td><%= pucgrupo.clase_id %></td>
17:     <td><%= pucgrupo.nombre %></td>
18:     <td><%= link_to 'Show', pucgrupo %></td>
19:     <td><%= link_to 'Edit', edit_pucgrupo_path(pucgrupo) %></td>
20:     <td><%= link_to 'Destroy', pucgrupo, :confirm => 'Are you sure?', :method => :delete %></td>
21:   </tr>

i'm enter rails console and typing

Pucgrupo.find_by_numero(1)
=> #<Pucgrupo numero: 1, clase_id: 2, nombre: "aaa", created_at: "2011-08-18 17:13:07", updated_at: "2011-08-18 17:13:07">


It seems you do not want use integer "id" as key, you have to change the

Pucgrupo.find(params[:id])

in action PucgruposController#show to

Pucgrupo.find_by_numero(params[:id])


The last error is given by the fact you've replaced the default primary key without notifying rails of it.

The error is generated by the line 18 in your view when the helper try to generate an URL for your show action. Try to add this line

primary_key :numero

to your model class. I this way you can use the find call and the issue should solve.

BTW as someone pointed out changing the default primary key name is often a bad idea which leads to this kind if issues.

Edit

To answer to your comment, yes, it's always better to follow the framework conventions, RoR is based on convention over configuration paradigm, so you should really follow that conventions unless you have a serious reason to change them (for example a legacy database with existing data). That said even when you change some convention you have to know all the consequences of doing that and you'll get that knowledge only with experience.

In your case the primary key is used in the find method, in URL generations, in foreign_key lookups and so on. Furthermore some third party gems could rely on the id field and may stop to work.

If you really want to change the primary key you must inform the model class of that (with the statement above) and the other models which has a fk relation with the involved class.


Well. You aren't creating an id column. If you want one, remove the following line

:id => false

See more here, under options for create_table

:id

Whether to automatically add a primary key column. Defaults to true. Join tables for has_and_belongs_to_many should set it to false.


if you post your controller I can be sure but rails defaults to finding records by the ID column.

The primary key has nothing to do with rails finding records.

So in your controller when you have

Pucgrupo.find(params[:id])

it is searching the table's id column for that id... you are removing that id column. The easy solution is to find_by

Pucgrupo.find_by_numero(params[:id])

This works but I strongly suggest you think about why you are removing the id column, remember all records and associations in rails depend on that column for defaults. By removing it you take what can be a beautiful association and make it ugly with find by's and foreign key references.

The id column is a good thing and if the only reason you are removing it is you want to localize table names with your language... keep it... it will make your life easier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜