gpt4 book ai didi

ruby-on-rails - Rails has_many :through with :primary_key

转载 作者:数据小太阳 更新时间:2023-10-29 08:00:24 24 4
gpt4 key购买 nike

我正在尝试通过以下方式创建与 rails has_many 的多对多关系:但我需要使用不同的列来创建关系,而不是使用模型主键 (id)。这是我的模型(顺便说一句,我使用的是 Rails 4):

class Food < ActiveRecord::Base
validates :NDB_No, uniqueness: true
validates :NDB_No, :FdGrp_Cd, :Long_Desc, :Shrt_Desc, presence: true

has_many :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
has_many :langual_factor_descriptions, through: :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
end

class LangualFactorAssociation < ActiveRecord::Base
validates :NDB_No, :Factor_Code, presence: true

belongs_to :food, foreign_key: 'NDB_No'
belongs_to :langual_factor_description, foreign_key: 'Factor_Code'
end

class LangualFactorDescription < ActiveRecord::Base
validates :Factor_Code, uniqueness: true
validates :Factor_Code, :Description, presence: true

has_many :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
has_many :foods, through: :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'

end

与 LangualFactorAssociation 的 has_many 关联适用于 Food 和 LangualFactorDescription。但是 has_many through: Food 和 LangualFactorDescription 之间的关联不起作用。这是我在尝试访问 Food.LangualFactorDescriptions 时遇到的错误:

Food::should create the proper relations to the LangualFactorDescription
model#test_0002_must create the proper associations:
ActiveRecord::StatementInvalid: PG::Error: ERROR: operator does not exist: integer = character varying
LINE 1: ...sociations" ON "langual_factor_descriptions"."id" = "langual...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "langual_factor_descriptions".* FROM "langual_factor_descriptions" INNER JOIN "langual_factor_associations" ON "langual_factor_descriptions"."id" = "langual_factor_associations"."Factor_Code" WHERE "langual_factor_associations"."NDB_No" = $1 ORDER BY "langual_factor_descriptions"."id" ASC LIMIT 1
test/models/food_test.rb:172:in `block (3 levels) in <top (required)>'

我认为问题出在 ON "langual_factor_descriptions"."id"= "langual_factor_associations"."Factor_Code" 查询的这一部分。我认为设置 primary_key 和/或 foreign_key 选项可以解决这个问题,但事实并非如此。事实上,如果我从模型中删除它们,然后就这样保留

has_many :langual_factor_descriptions, through: :langual_factor_associations

rails 产生完全相同的查询,所以在我看来设置这些选项没有任何作用。我在这里错过了什么吗?关于如何告诉 Rails 不要查找 langual_factor_descriptions.id 而是查找 langual_factor_descriptions.Factor_Code,有什么想法吗?

以下是我阅读过的关于该主题的一些最相关的资源: http://guides.rubyonrails.org/association_basics.html

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

has_many :through with :primary_key on join table not working (这几乎是我遇到的问题,但我不确定这是正确的解决方案)

Rails has_many association with multiple keys

https://www.ruby-forum.com/topic/139765

Has Many Through Alternative Primary and Foreign Keys

最佳答案

我想我解决了这个问题。这是代码:

class Food < ActiveRecord::Base
validates :NDB_No, uniqueness: true
validates :NDB_No, :FdGrp_Cd, :Long_Desc, :Shrt_Desc, presence: true

has_many :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
has_many :langual_factors, through: :langual_factor_associations
end

class LangualFactorAssociation < ActiveRecord::Base
validates :NDB_No, :Factor_Code, presence: true

belongs_to :food, primary_key: 'NDB_No', foreign_key: 'NDB_No'
belongs_to :langual_factor, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
end

class LangualFactor < ActiveRecord::Base
validates :Factor_Code, uniqueness: true
validates :Factor_Code, :Description, presence: true

has_many :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
has_many :foods, through: :langual_factor_associations
end

请注意,我没有在 has_many through: 关联中使用 foreign_key 或 primary_key 选项,并且不需要 self.primary_key

此外,这里还有一些对我有帮助的其他有用链接:

http://railsforum.com/viewtopic.php?id=36186

http://guides.rubyonrails.org/v2.3.11/active_record_querying.html

尽管问题的解决是巧合,但我相信这些链接提供了相关信息。

关于ruby-on-rails - Rails has_many :through with :primary_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17666690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com