lundi 9 janvier 2017

Ruby on Rails: Display Drop Down Menu with Data from another Model

I have a Ruby on Rails system whereby I have the tables: Users, UserAccounts, Accounts, and Emails with the schemas:

Users (id, firstname, surname, password)
UserAccounts (user_id, account_id)
Accounts (id, emailaddress, port, domain)
Emails (id, account_id, subject, message)

What I am trying to do is when creating new email, I want all of the user's accounts' emailaddress variable to display on the view in a drop down menu and when the user clicks save I want the account_id to be saved in the Emails table.

At the minute I can display the account_id in a drop down menu on the views/emails/_form.html.erb with the following line of code:

<%= f.collection_select :account_id, Useraccount.where(user_id: session[:user_id]), :account_id, :account_id %>

But I cannot change this to show the email address, I have tried the following code to do so:

<%= f.collection_select :account_id, Useraccount.where(user_id: session[:user_id]), :account_id, :Account.where(id: account_id).email %>

But this gives me the error undefined local variable or methodaccount_id' for #<#:0x72cde60>`

Can anyone please help?

Aucun commentaire:

Enregistrer un commentaire