Ruby on rails Rails:如何以特定的方式命名模型的属性?
我正在寻找解决我的小问题的办法——也许你想帮忙^^ 在RubyonRails中,我将类建模为“Person”和“Contact”。一个人可以有许多联系人,一个联系人可以有一个特定的人,并用一个值来描述这种关系Ruby on rails Rails:如何以特定的方式命名模型的属性?,ruby-on-rails,model,attributes,rename,Ruby On Rails,Model,Attributes,Rename,我正在寻找解决我的小问题的办法——也许你想帮忙^^ 在RubyonRails中,我将类建模为“Person”和“Contact”。一个人可以有许多联系人,一个联系人可以有一个特定的人,并用一个值来描述这种关系 class Person < ActiveRecord::Base has_many :contacts end class Contact < ActiveRecord::Base belongs_to :person has_one :person #ho
class Person < ActiveRecord::Base
has_many :contacts
end
class Contact < ActiveRecord::Base
belongs_to :person
has_one :person #how to rename this?
end
class-Person
在Person的表中没有什么特殊的,或者与contact相关的列,但是contact表脚本如下所示
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.references :person
t.references :person #how to rename this eather?
t.integer :value
end
end
def self.down
drop_table :contacts
end
end
class CreateContacts
正如我在源代码中所写的——我不知道如何将第二个关系重命名为person——如果您能给我一个提示,我将非常感激=)
问候
Klaf这个呢:
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.references :person
t.string :person_description
t.integer :value
end
end
def self.down
drop_table :contacts
end
end
那么这个呢:
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.references :person
t.string :person_description
t.integer :value
end
end
def self.down
drop_table :contacts
end
end
我会将:belishing\u改为'owner',将:has\u one改为'person'。我会将:belishing\u改为'owner',将:has\u one改为'person'。在您的
联系人
模型中,您不需要额外的has\u one x
关系,因为由于属于:个人
协会
请注意,您的
联系人
表应该有一个person\u id
整数字段作为外键。您不需要在联系人
模型中附加的has one x
关系,因为属于:person
协会
class Person < ActiveRecord::Base
has_many :contacts
end
class Contact < ActiveRecord::Base
belongs_to :person
belongs_to :contact, :class_name => "Person"
end
#in migration
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.references :person
t.integer :contact_id
t.integer :value
end
end
def self.down
drop_table :contacts
end
end
请注意,您的联系人
表应该有一个个人id
整数字段作为外键。类个人class Person < ActiveRecord::Base
has_many :contacts
end
class Contact < ActiveRecord::Base
belongs_to :person
belongs_to :contact, :class_name => "Person"
end
#in migration
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.references :person
t.integer :contact_id
t.integer :value
end
end
def self.down
drop_table :contacts
end
end
你有很多联系人吗
结束
类联系人class-Person“个人”
结束
#迁移中
类CreateContacts