gpt4 book ai didi

ruby-on-rails - 通过...处理 has_many 的 rails 中的关联...但是两个连接表很深

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:05 25 4
gpt4 key购买 nike

编辑 4. 我的解释中有些地方不对...附上一张(做得很差的图形),它显示了表格和模式,然后我的主要问题是我有什么供应商通过 provider_location 表通过组位置 ID 加入,组位置包含组,最后是组名。这就是我想知道的,建一个这样的表如何获取组名?一直来自供应商。这就像我需要一个 has_many 通过。 (重要提示:图中的 Provider Address 和 Group Address 实际上是 Provider Location 和 Group Location)

rough model diagram.  a provider can have many addresses to many group locations.  Generally a group has many addresses

编辑 x 3:谢谢@mrshoco。它让我更接近了,现在它就像我的结构不太正确的其他东西......我在运行 provider_test_location.rb 时收到此错误

vagrant@precise32:/vagrant/ipanmv2$ rake test test/models/provider_location_tes
t.rb
Run options: --seed 18117

# Running:

E

Finished in 0.190900s, 5.2383 runs/s, 5.2383 assertions/s.

1) Error:
ProviderLocationTest#test_fetching_a_group_name_for_a_provider:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: provider_
locations.group_id: SELECT groups.group_name FROM "groups" INNER JOIN "provider_
locations" ON "provider_locations"."group_id" = "groups"."id" INNER JOIN "provid
ers" ON "providers"."id" = "provider_locations"."provider_id" WHERE "providers".
"first_name" = 'Shane'
test/models/provider_location_test.rb:37:in `puts'
test/models/provider_location_test.rb:37:in `puts'
test/models/provider_location_test.rb:37:in `block in <class:ProviderLocatio
nTest>'

1 runs, 1 assertions, 0 failures, 1 errors, 0 skips

这是完整的单元测试

require 'test_helper'

class ProviderLocationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

test "adding a provider to a group location" do
group = Group.new
group.group_name = 'My Awesome'
group.save
adr = Address.new
adr.city = 'Las Cruces'
adr.state = 'New Mexico'
adr.zip = '88012'
adr.address_line_one = '382 Dark side of moon'
adr.address_line_two = ''
adr.save

gl = GroupLocation.new
gl.group = group
gl.address=adr
gl.save


prv = Provider.new
prv.first_name = 'Shane'
prv.last_name = 'Thomas'
prv.save

pl = ProviderLocation.new
pl.provider = prv
pl.group_location = gl ###############ISSUE

assert pl.save, 'Provider location did not save'

puts Group.joins(:providers).where(providers: { first_name: 'Shane' }).select('groups.group_name')




end
end

模型如下:

class Provider < ActiveRecord::Base
belongs_to :designation
belongs_to :specialty
has_many :provider_locations
has_many :invoices
has_many :groups, through: :provider_locations
end

class ProviderLocation < ActiveRecord::Base
belongs_to :provider
belongs_to :group_location
end

class Group < ActiveRecord::Base

#validations
validates :group_name, presence: true


#associations
has_many :providers, through: :provider_locations
has_many :invoices
has_one :billing
has_many :addresses, through: :group_locations


has_many :group_locations
belongs_to :billing_address, class_name: 'Address', foreign_key: 'billing_address_id'
belongs_to :mailing_address, class_name: 'Address', foreign_key: 'mailing_address_id'
has_and_belongs_to_many :insurances
has_many :provider_locations



end


class GroupLocation < ActiveRecord::Base
belongs_to :address
belongs_to :group

end

class Address < ActiveRecord::Base
has_many :group_locations
has_many :billings
end

最佳答案

# Group name given a provider name
Group.joins(:providers).where('providers.first_name' => 'Shane').select('groups.name')

# address/location of the provider given the group name
Provider.joins(:groups).where("groups.name" => "your_group_name").first.provider_locations.first

关于ruby-on-rails - 通过...处理 has_many 的 rails 中的关联...但是两个连接表很深,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260679/

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