我在注册表之间有多对一的关系...
class Registry < ActiveRecord::Base
has_many :user_registries
attr_accessible :logo, :name
has_attached_file :logo
end还有user_registries..。
class UserRegistry < ActiveRecord::Base
belongs_to :page
has_one :registry
attr_accessible :page_id, :registry_id, :url
end我尝试在表单中显示名称或徽标,如下所示:
.registry
= debug f.object.registry.name
.field
= f.label :title
= f.text_field :title
.field
= f.label :url
= f.text_field :url
.field
= f.hidden_field :_destroy
= link_to_function "remove", "remove_fields(this)"但我收到一个SQL错误,如下所示:
Mysql2::Error: Unknown column 'registries.user_registry_id' in 'where clause': SELECT `registries`.* FROM `registries` WHERE `registries`.`user_registry_id` = 14 LIMIT 1我的关系设置不正确吗?
发布于 2013-01-31 06:42:02
我的关系实际上是不正确的。
class UserRegistry < ActiveRecord::Base
belongs_to :page
has_one :registry
attr_accessible :page_id, :registry_id, :url
end外键在用户注册表中,所以我需要belongs_to而不是has_one关系,如下所示。
class UserRegistry < ActiveRecord::Base
belongs_to :page
belongs_to :registry
attr_accessible :page_id, :registry_id, :url
end发布于 2013-01-30 06:52:19
我真的不认为这和关系有任何关系。您可以确保在表“注册表”中有一列"user_registry_id“,而且在您的情况下,我认为它应该被设置为外键。
https://stackoverflow.com/questions/14593486
复制相似问题