Foreign keys in order to send email

Hi all,

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

Assuming you have specified user has_many games and game belongs_to user then if you have a game you can access the game's user by game.user, so you can access the users email by game.user.email.

Have a look at the Rails Guide on ActiveRecord Associations (and the other guides too). As it seems you need to learn about Rails basics I suggest that you work right through a tutorial such as railstutorial.org (which is free to use online). But wait, have I not suggested this to you on at least one previous occasion? Have you done that? Am I wasting my time I wonder?

Colin

Colin Law wrote in post #1050562:

I currently have the following in my games_controller for the show section so that when a user clicks on the button it does the following e.g. Jon(user 6) is interested in Adam(user 31) game. Jon clicks on adams game show page and clicks the interested button. The email should send to user 31's email address.

Now if I write game.user.email I get undefined method game but the following states wrong number of arguments (1 of 0).

  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

I currently have the following in my games_controller for the show section so that when a user clicks on the button it does the following e.g. Jon(user 6) is interested in Adam(user 31) game. Jon clicks on adams game show page and clicks the interested button. The email should send to user 31's email address.

Now if I write game.user.email I get undefined method game but the following states wrong number of arguments (1 of 0).

Obviously in my example I was assuming that the variable game held a game object.

def show @game = Game.find(params[:id])

respond_to do |format| GameTrade.game_interest(@game.user.email).deliver

If it is not obvious which method it is complaining about then split this into a number of lines, something like user = @game.user email = user.email g = GameTrade.game_interest(email) g.deliver

and then you will see which line it fails on, assuming it is this bit at all, but I assume you have looked at the error to see which line is causing the problem.

Also have a look at the Rails Guide on Debugging to see how to debug your code.

Colin

If it is not obvious which method it is complaining about then split this into a number of lines, something like user = @game.user email = user.email g = GameTrade.game_interest(email) g.deliver

and then you will see which line it fails on, assuming it is this bit at all, but I assume you have looked at the error to see which line is causing the problem.

Also have a look at the Rails Guide on Debugging to see how to debug your code.

Hey Colin I done as you said and the debugger states the following error:

wrong number of arguments (1 for 0)

and then states the following:

app/mailers/game_trade.rb:9:in `game_interest' app/controllers/games_controller.rb:19:in `block in show' app/controllers/games_controller.rb:16:in `show'

Now the first one shouldn't be a problem, it is just the mailer as I have as followed which is fine:

  def game_interest     @user = user     mail :to => user.email, :subject => "Game Interest"   end

But the second set of problems gives insight in to what the problem would be. The problem is line 19 in the controller which is the following line:

g = GameTrade.game_interest(email)

So you are calling game_interest with a parameter email. Read the error. It says that you are passing 1 parameter when it expects 0. Have a look at the spec of game_interest, how many parameters does it take?

How are you getting on with railstutorial.org? If you worked right through it, including the exercises and so on, then you would not have to keep asking basic questions.

Colin

Colin Law wrote in post #1050576:

You do realize that the entire thing is free to READ, right? The podcasts add flavor and show someone else doing the exercise, but the entire course is available for you to read and do completely FOC.

Walter

Colin Law wrote in post #1050576:

app/mailers/game_trade.rb:9:in `game_interest'

But the second set of problems gives insight in to what the problem would be. The problem is line 19 in the controller which is the following line:

g = GameTrade.game_interest(email)

So you are calling game_interest with a parameter email. Read the error. It says that you are passing 1 parameter when it expects 0. Have a look at the spec of game_interest, how many parameters does it take?

How are you getting on with railstutorial.org? If you worked right through it, including the exercises and so on, then you would not have to keep asking basic questions.

Colin

Hey Colin

What I have in my game_interest.rb is what I stated in an above post a few posts back.

Please don't keep deleting the context. It was in my reply, you have just deleted it. You have def game_interest    @user = user    mail :to => user.email, :subject => "Game Interest" end

How many parameters does that take? Please answer this question.

The code that is generating the error is g = GameTrade.game_interest(email)

How many parameters are you passing. Please answer this question.

Is the answer to the first question the same as the answer to the second?

I have only worked through the free tutorials on railstutorial.org as I can't afford the purchase of each episode Only managed to work through tutorials 1 and 8 (as they were free).

The whole thing is free to use online. From http://ruby.railstutorial.org/ click on "is available for free online" in the second paragraph. Now go and spend a few days working through it.

Colin

But actually those are not even the same method. One is a class method and one is an instance method. Have you defined both? Which one are you trying to use? If you do not understand the difference between class and instance methods in ruby then do a bit of googling into the basics of Ruby.

Colin

Colin Law wrote in post #1050581:

Hey Colin

What I have in my game_interest.rb is what I stated in an above post a few posts back.

Please don't keep deleting the context. It was in my reply, you have just deleted it. You have def game_interest    @user = user    mail :to => user.email, :subject => "Game Interest" end

How many parameters does that take? Please answer this question.

The code that is generating the error is g = GameTrade.game_interest(email)

How many parameters are you passing. Please answer this question.

Is the answer to the first question the same as the answer to the second?

I have only worked through the free tutorials on railstutorial.org as I can't afford the purchase of each episode Only managed to work through tutorials 1 and 8 (as they were free).

The whole thing is free to use online. From http://ruby.railstutorial.org/ click on "is available for free online" in the second paragraph. Now go and spend a few days working through it.

Colin

I see, that makes it a lot better, I will begin working through them after my lunch. Should give further insight in to some sections. A lot I have already covered through my book.

As an update I noticed I was not including anything after def game_interest in the mailer so I added (user) to the end of it which gives me a new error now which is the following:

undefined method `email' for "chris230391@googlemail.com":String

This shows it is at least getting to a stage as the user who I want to send the email to is infact that email address.

That says that you are trying to call method email on something that is a string rather than a User object.

Colin

Fixed my problem now. I changed the line:

g = GameTrade.game_interest(email)

to include user in the brackets to call the object.

All I need to do now is to put the submit_tag in to a form in order to get it to actually work.