Ruby-on-rails 带有自定义外键的 Rails own_to has_many

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16876462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:05:56  来源:igfitidea点击:

Rails belong_to has_many with a custom foreign key

ruby-on-rails

提问by Drahcir

I have a relationship between 2 models in my rails application. I have veered away from the standard of how to implement the relationship as I used another field as a primary key and the naming convention is different. Doing so resulted in the relationship seemingly not being established. I want to understand as to why.

我的 rails 应用程序中有 2 个模型之间的关系。我已经偏离了如何实现关系的标准,因为我使用另一个字段作为主键并且命名约定不同。这样做导致这种关系似乎没有建立。我想了解为什么。

This is a trimmed down version of my models:

这是我的模型的精简版:

class Player < ActiveRecord::Base
  set_primary_key "alias"
  attr_accessible :alias, :avatar
  has_many :player_sessions, :foreign_key => "player_alias", :class_name => "PlayerSession" 
end

class PlayerSession < ActiveRecord::Base
  attr_accessible :player_alias, :total_score
  belongs_to :player, :foreign_key  => "player_alias", :class_name => "Player" 
end

The Playermodel has the field aliaswhich is the username in my application. I wanted the username to act as the primary key since it is unique and it would be easier to migrate the data and maintain relationships.

Player模型的字段alias是我的应用程序中的用户名。我希望用户名充当主键,因为它是唯一的,并且可以更轻松地迁移数据和维护关系。

Originally I only had PlayerSession model with data already filled in, but as my application grew I added the Player model and simply inserted a row with the same alias.

最初我只有已经填充了数据的 PlayerSession 模型,但是随着我的应用程序的增长,我添加了 Player 模型并简单地插入了一行具有相同alias.

In the Player's showview I have the following code:

Playershow视图中,我有以下代码:

Player Sessions:
<% @player.player_sessions do |player_session| %>
<ul>
    <li><h4>Highest Score:</h4> <%= player_session.total_score %> </li>
</ul>

When I try to access the page it simple doesn't show the information.

当我尝试访问该页面时,它很简单不显示信息。

Other information I can add is that I haven't added any relationships in the database itself.

我可以添加的其他信息是我没有在数据库本身中添加任何关系。

I am still new to rails and still playing around with it. Any opinions that concern coding standards(outside from answering the question) are welcome.

我对 Rails 还是个新手,还在玩它。欢迎任何有关编码标准的意见(除了回答问题)。



UpdateI have implemented Babur Usenakunov's suggestion by adding the primary_keyoption in the models:

更新我通过primary_key在模型中添加选项来实现 Babur Usenakunov 的建议:

class Player < ActiveRecord::Base
  set_primary_key "alias"
  attr_accessible :alias, :avatar
  has_many :player_sessions, :primary_key => "alias", :foreign_key => "player_alias", :class_name => "PlayerSession" 
end

class PlayerSession < ActiveRecord::Base
  attr_accessible :player_alias, :total_score
  belongs_to :player, :primary_key => "alias", :foreign_key  => "player_alias", :class_name => "Player" 
end

Also to test out the the data is valid I acquired the PlayerSession list manually:

同样为了测试数据是否有效,我手动获取了 PlayerSession 列表:

Code implemented in controller:

在控制器中实现的代码:

@player_sessions =  PlayerSession.where("player_alias = ?", params[:id])

Code implemented in view(which outputs the data):

在视图中实现的代码(输出数据):

<% @player_sessions.each do |player_session| %>
<ul>
    <li><h4>Highest Score:</h4> <%= player_session.total_score %> </li>
</ul>
<% end %>

采纳答案by Drahcir

I have solved the issue, it was a matter of adding eachin the loop I implemented in the view:

我已经解决了这个问题,这是each在我在视图中实现的循环中添加的问题:

<% @player.player_sessions.each do |player_session| %>
<ul>
    <li><h4>Highest Score:</h4> <%= player_session.total_score %> </li>
</ul>    
<% end %>

After playing around a bit, I realised i didn't need to add the primary_key option in either of the views, and left the foreign key option only in the Playermodel.

玩了一会后,我意识到我不需要在任何一个视图中添加 primary_key 选项,而只在Player模型中保留外键选项。

class Player < ActiveRecord::Base
  set_primary_key "alias"
  attr_accessible :alias, :avatar
  has_many :player_sessions, :foreign_key => "player_alias", :class_name => "PlayerSession" 
end

class PlayerSession < ActiveRecord::Base
  attr_accessible :player_alias, :total_score
  belongs_to :player, :class_name => "Player" 
end

回答by marcelowiermann

If your PlayerSession table has a column named aliasthen your foreign key should also be alias, not player_alias. As a token of advice I'd be wary of using an alias as a foreign_key: if your player can/decide to change his alias then all PlayerSession records within your database will become invalid and require an update. Using an immutable parameter such as player_idwould be preferable to using the existing alias column IMHO.

如果您的 PlayerSession 表有一个名为的列,alias那么您的外键也应该是alias,而不是player_alias。作为建议,我会谨慎使用别名作为外键:如果您的玩家可以/决定更改他的别名,那么您数据库中的所有 PlayerSession 记录都将无效并需要更新。使用不可变参数,例如player_id使用现有别名列恕我直言更可取。