简体   繁体   English

Ruby on Rails活动记录联接

[英]Ruby on Rails Active Record Join

I need to joins this two tables but I cant seems to get it right. 我需要加入这两个表,但似乎无法正确处理。

Model 模型

class Registration < ActiveRecord::Base has_many :region end

class Region < ActiveRecord::Base belongs_to :registration end

what I have tried so far is this 到目前为止,我尝试过的是

`test = Region.joins(:registration)`

which will give me this result 这会给我这个结果

[#<Region id: 1, name: "region1">, #<Region id: 1, name: "region1">]

It seem that i have certainly did join the two tables but my dilemma is I also need the index that belongs to Registration Model. 看来我确实已经加入了两个表,但是我的困境是我还需要属于注册模型的索引。 the about results are only the index available in the Region Model. 关于结果只是区域模型中可用的索引。

I also tried it the other way around 我也尝试了另一种方法

test = Registration.joins(:region)

but it gives me a error 但这给我一个错误

Unknown column 'regions.registration_id' in 'on clause': SELECT registrations .* FROM registrations INNER JOIN regions ON regions . Unknown column 'regions.registration_id' in 'on clause': SELECT注册表.* FROM注册表INNER JOIN区域ON区域. registration_id = registrations . registration_id =注册. id`` id

and I do agree with this error cause i do not have a index registration_id 我同意这个错误,因为我没有索引registration_id

when you define belongs_to relationship in a table that table should have foreign_key of table to which it is belonging. 当您在表中定义belongs_to关系时,该表应具有其所属表的foreign_key。 So in your example if you have relation like Region belongs to registrations then regions should have registration_id. 因此,在您的示例中,如果您有诸如Region属于注册的关系,则Region应该具有registration_id。 It looks like you have defined a wrong relationship. 您似乎定义了错误的关系。

So if you will define registration_id in Region and execute following query: 因此,如果您将在Region中定义registration_id并执行以下查询:

Region.joins(:registration) 

You will see id of region table along with registration_id in the result 您将在结果中看到区域表的ID和registration_id

Hope this helps 希望这可以帮助

Change the Registration model code 更改Registration model代码

class Registration < ActiveRecord::Base
  has_many :regions
end

Then you can use joins 然后您可以使用joins

test = Registration.joins(:region)

Hope it helps! 希望能帮助到你!

class Registration < ActiveRecord::Base
    has_many :regions, inverse_of: :registration, dependent: :destroy
 end

class Region < ActiveRecord::Base
    belongs_to :registration, inverse_of: :regions
end

Then, make sure your Region table correctly shows its association to Registration with a registration_id column. 然后,确保您的Region表正确地使用registration_id列显示了与Registration关联。 If it doesn't contain the column, go ahead and create the following migration: 如果它不包含该列,请继续进行以下迁移:

rails g migration add_registration_ref_to_regions registration:references

rake db:migrate

Now, you should be able to successfully query via: 现在,您应该能够通过以下方式成功查询:

Registration.joins(:regions) or Region.joins(:registration) Registration.joins(:regions)Region.joins(:registration)

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM