Reputation: 553
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.
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.
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?
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 "[email protected]":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:
undefined method `email' for "[email protected]":String
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:
class GameTrade < ActionMailer::Base
default :from => "[email protected]"
def game_interest(user)
@user = user
mail :to => user.email, :subject => "Game Interest"
end
end
Upvotes: 0
Views: 1519
Reputation: 96454
The line GameTrade.game_interest(@game.user.email).deliver
calls the game_interest method in the GameTrade model.
(Please post that code also.)
This method expects a parameter and it's not getting it.
I would also consider making both the method and the call be for
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.
Upvotes: 1
Reputation: 14983
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.
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?
Upvotes: 2