Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Table Naming Convention


  • Please log in to reply
6 replies to this topic

#1 cmetzner

cmetzner

    Passenger

  • Members
  • 6 posts

Posted 10 January 2014 - 11:10 AM

class CreatePlayers < ActiveRecord::Migration

  def change

    create_table :players do |t|

      t.string :firstname

      t.string :lastname

      t.integer...

      t.integer :player_detail_id

 

      t.timestamps

    end

  end

end

 

class CreatePlayerDetails < ActiveRecord::Migration

  def change

    create_table :player_details do |t|

      ...

      

      t.timestamps

    end

  end

end

 

class PlayerDetail < ActiveRecord::Base

  belongs_to :player

end

 

class Player < ActiveRecord::Base

  has_many :player_details

end

 

 

p=Player.create(:firstname => "Michael", :lastname => "Hoffmann", ...)

p.create_player_detail(...)

 
I get the following error message: undefined method `create_player_detail' for #<Player:0x0000010508adf8>
 
What is the problem? Associations and table names should be right, isn't it?


#2 RoyTheB0y

RoyTheB0y

    Signalman

  • Members
  • 10 posts

Posted 10 January 2014 - 11:44 AM

shouldn't it be

 

p.player_details.create(...)



#3 cmetzner

cmetzner

    Passenger

  • Members
  • 6 posts

Posted 10 January 2014 - 11:48 AM

no, it also doesn't work



#4 RoyTheB0y

RoyTheB0y

    Signalman

  • Members
  • 10 posts

Posted 10 January 2014 - 12:02 PM

What's the error ?

 

I think you have the migrations wrong

 

Class PlayerDetail ActiveRecord::Base

  belongs_to :player

end

 

means that PlayerDetail should have a column player_id

 

so remove 
 t.integer :player_detail_id
from Player
 
and add
 t.integer :player_id
to PlayerDetail
 
check out 
 
 
 


#5 cmetzner

cmetzner

    Passenger

  • Members
  • 6 posts

Posted 10 January 2014 - 01:04 PM

it still doesn't work! same error: undefined method `create_player_detail' for #<Player:0x0000010508adf8>

 

it only works after the following changes:

 

class PlayerDetail ActiveRecord::Base

  belongs_to :player

end

 

class Player ActiveRecord::Base

  has_one :player_detail

end

 

class CreatePlayers ActiveRecord::Migration

  def change

    create_table :players do |t|

       ...

    end

  end

end

 

class CreatePlayerDetails ActiveRecord::Migration

  def change

    create_table :player_details do |t|

      t.integer :player_id

      ...

    end

  end

end

 

but in this case every player only can have one player-detail, but every matchday a player receive new player-details and so a new insert in the player-detail-table must happen.



#6 RoyTheB0y

RoyTheB0y

    Signalman

  • Members
  • 10 posts

Posted 10 January 2014 - 01:17 PM

are you using 

p.player_details.create(...) rather than p.create_player_detail(...)

 

what is the line of code that generates this error. I think the error message is correct rails, will not create a method create_player_detail, p.player_details.create(...) is the way that works for me

 

class Player ActiveRecord::Base

  has_one :player_detail

end

 

should be

 

class Player ActiveRecord::Base

  has_many :player_details  #note the plural

end

 

 

 



#7 cmetzner

cmetzner

    Passenger

  • Members
  • 6 posts

Posted 10 January 2014 - 02:09 PM

okay. thanks. it works






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users