Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: undefined method `primary_key' for String:Class

I have 3 classes, Schema, Entity and Property, representing the DB design business. The Schema-Entity seems work, but Entity-Property does not.

class Hx::Entity < ActiveRecord::Base
  belongs_to :schema
  attr_accessible :name
  has_many :properties    ,  class_name: "Hx::Property"  , primary_key: "id"
end

class Hx::Property < ActiveRecord::Base
  attr_accessible :destination, :indexed, :inverse, :isToMany, :kind, :name, :optional, :transient  , :type


  belongs_to :entity
end

When I run entity_obj.properties, it throws the error undefined method primary_key' for String:Class.

I twist around the has_many's options, but it does not help.

Does anyone has any idea on this?

Thanks.

like image 746
Juguang Avatar asked Mar 20 '13 07:03

Juguang


2 Answers

Thank you muttonlamb!

I figured out the problem.

In the beginning, I guess the problem is around has_many since that is where it appears. But it is not the case. even i do not define class_name, Rails still can find the class.

Later on I found some record show up that the attribute type in Property is not assigned a value. The root cause is that I override superclass's attribute !

The solution:

ActiveRecord::Migration.rename_column :hx_properties , :type, :datatype
like image 137
Juguang Avatar answered Nov 08 '22 20:11

Juguang


I think you need to drop the "" around HX::Property

The error message is telling you that it is trying to call primary key on an object of String:Class

like image 35
muttonlamb Avatar answered Nov 08 '22 18:11

muttonlamb