I have a problem at the moment and am not sure what I should do. It is in regards to relationships between tables and whether I need foreign keys.
我现在有问题,不知道该怎么做。这是关于表之间的关系以及我是否需要外键。
I have two tables in my database; users and games. Users includes all the users information (username, email, address, password etc.) Games includes all the games information (name, genre, console user_id). As you may have noticed the games table includes the users id in order to associate the relationship between the two which is every game needs an associated user.
我的数据库中有两个表;用户和游戏。用户包括所有用户信息(用户名,电子邮件,地址,密码等)。游戏包括所有游戏信息(名称,流派,控制台user_id)。您可能已经注意到,游戏表包含用户ID,以便关联两者之间的关系,即每个游戏都需要关联用户。
What I wish to do is on each games show.html.erb page, I have a button which should send an email to the owner of that game in order to let them now that the currently signed in user is interested in trading that game.
我想做的是在每个游戏show.html.erb页面上,我有一个按钮,应该向该游戏的所有者发送一封电子邮件,以便让他们现在已经登录的用户有兴趣交易该游戏。
I am wondering how I would do this. Does the user_id automatically link all the users associated data or just the id. If not then how would I declare the users email as a foreign key in order for me to call the users email to be sent to?
我想知道我会怎么做。 user_id是否自动链接所有用户关联数据或仅ID。如果没有,那么我如何将用户的电子邮件声明为外键,以便我将用户的电子邮件发送给?
Any help would be much appreciated. Thank you
任何帮助将非常感激。谢谢
Christopher Jones
UPDATE
Hey I had the following in my games_controller show section and it did not work. It threw the following error: undefined method `email' for "chris230391@googlemail.com":String.
嘿我在我的games_controller show部分中有以下内容,但它没有用。它引发了以下错误:“chris230391@googlemail.com”的未定义方法`email':String。
def show
@game = Game.find(params[:id])
respond_to do |format|
GameTrade.game_interest(@game.user.email).deliver
format.html { redirect_to root_url }
format.json { render json: @game }
end
end
So I changed it to the following in order to break down the problem and find out what line it broke on and it stated that the block was on line 19 which is the line g = GameTrade.game_interest(email). I got the following error:
因此我将其更改为以下内容,以便分解问题并找出它突破的线路,并且表明该块位于第19行,即g = GameTrade.game_interest(电子邮件)。我收到以下错误:
undefined method `email' for "chris230391@googlemail.com":String
undefined方法`email'为“chris230391@googlemail.com”:字符串
Any ideas? the code is below
有任何想法吗?代码如下
def show
@game = Game.find(params[:id])
respond_to do |format|
user = @game.user
email = user.email
g = GameTrade.game_interest(email)
g.deliver
format.html { redirect_to root_url }
format.json { render json: @game }
end
end
The following code is that of my Game_trade.rb in the mailer section:
以下代码是我在邮件程序部分中的Game_trade.rb的代码:
class GameTrade < ActionMailer::Base
default :from => "christopher@aol.com"
def game_interest(user)
@user = user
mail :to => user.email, :subject => "Game Interest"
end
end
2 个解决方案
#1
2
Take a look at this Stack Overflow question regarding Rails and referential integrity. You may want to look into the foreigner gem for adding and removing foreign keys during Rails migrations.
看一下有关Rails和参照完整性的Stack Overflow问题。您可能希望查看外国人gem以在Rails迁移期间添加和删除外键。
However, I'm curious as to why you're creating foreign key constraints beyond the game->user constraint you already have. Is there a reason you're denormalizing the username and email fields?
但是,我很好奇你为什么要在游戏 - >用户约束之外创建外键约束。您是否有理由对用户名和电子邮件字段进行非规范化?
#2
1
The line GameTrade.game_interest(@game.user.email).deliver
calls the game_interest method in the GameTrade model.
(Please post that code also.)
GameTrade.game_interest(@ game.user.email).deliver这一行调用了GameTrade模型中的game_interest方法。 (请发布该代码。)
This method expects a parameter and it's not getting it.
这个方法需要一个参数而它没有得到它。
I would also consider making both the method and the call be forgame_interest_deliver(@game.user.email).
我还会考虑将方法和调用都用于game_interest_deliver(@ game.user.email)。
I worked in sql data warehousing for 5 years and I was very confused by rails and foreign keys initially. The main lesson I've learned is not add them as appropriate but focus more on the ruby code and models and method to achieve the right results.
我在sql数据仓库工作了5年,我最初对rails和外键非常困惑。我学到的主要教训不是在适当的时候添加它们,而是更多地关注ruby代码以及模型和方法以获得正确的结果。
#1
2
Take a look at this Stack Overflow question regarding Rails and referential integrity. You may want to look into the foreigner gem for adding and removing foreign keys during Rails migrations.
看一下有关Rails和参照完整性的Stack Overflow问题。您可能希望查看外国人gem以在Rails迁移期间添加和删除外键。
However, I'm curious as to why you're creating foreign key constraints beyond the game->user constraint you already have. Is there a reason you're denormalizing the username and email fields?
但是,我很好奇你为什么要在游戏 - >用户约束之外创建外键约束。您是否有理由对用户名和电子邮件字段进行非规范化?
#2
1
The line GameTrade.game_interest(@game.user.email).deliver
calls the game_interest method in the GameTrade model.
(Please post that code also.)
GameTrade.game_interest(@ game.user.email).deliver这一行调用了GameTrade模型中的game_interest方法。 (请发布该代码。)
This method expects a parameter and it's not getting it.
这个方法需要一个参数而它没有得到它。
I would also consider making both the method and the call be forgame_interest_deliver(@game.user.email).
我还会考虑将方法和调用都用于game_interest_deliver(@ game.user.email)。
I worked in sql data warehousing for 5 years and I was very confused by rails and foreign keys initially. The main lesson I've learned is not add them as appropriate but focus more on the ruby code and models and method to achieve the right results.
我在sql数据仓库工作了5年,我最初对rails和外键非常困惑。我学到的主要教训不是在适当的时候添加它们,而是更多地关注ruby代码以及模型和方法以获得正确的结果。