Ruby on Rails:使用函数从模型中检索数据
Ruby on Rails: Using functions to retrieve data from model
在我的 RoR 应用程序中,我试图在视图上显示来自关联表的信息。
我有这样的数据表:
Email
id subject
1 This week's work
2 Presentation
3 Spreadsheets
Recipients
email_id group_id
1 2
2 2
3 1
1 3
Groups
id name
1 Managers
2 Employees
3 Directors
Contactgroups
group_id contact_id
1 1
2 1
1 3
3 2
Contacts
id email
1 Gary
2 Dave
3 Annie
我想做的是显示收到电子邮件的组列表以及每个组成员。例如,电子邮件 1 "This week's work" 被发送到组 1 "Managers" 和 3 "Directors","Managers" 组由联系人 Gary 和 Annie 以及 "Directors" 组组成由联系人 "Dave" 组成。因此,在 show.html.erb
页面上,我想显示电子邮件的详细信息以及它被发送到的组和他们的每个联系人。
为了做到这一点,在 show.html.erb
页面上我有代码:
<p>
<strong>Groups Sent To:</strong></br>
<% @recipients.each do |recipient| %>
<%= recipient.group.name %>
<% for i in 0..count_group_members(recipient.group.id)-1 do %>
<%= group_member_name(recipient.group.id, i) %>
<% end %></br>
<% end %>
</p>
在我的 emails_controller
中有代码:
class EmailsController < ApplicationController
helper_method :count_group_members, :group_member_name
def index
@useraccounts = Useraccount.where(user_id: session[:user_id])
accountsarray = [0]
@useraccounts.each do |f|
accountsarray << f.account_id
end
# find all emails where the account_id is in the array - and so belongs to the user
@emails = Email.where(account_id: [accountsarray])
end
def show
@email = Email.find(params[:id])
@account = Account.find_by_id(@email.account_id)
@recipients = Recipient.where(email_id: @email.id)
end
def new
@email = Email.new
@email.recipients.build
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def edit
@email = Email.find(params[:id])
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def create
@email = Email.new(email_params)
if @email.save
redirect_to @email
else
render 'new'
end
end
def update
@email = Email.find(params[:id])
if @email.update(email_params)
redirect_to @email
else
render 'edit'
end
end
def destroy
@email = Email.find(params[:id])
@email.destroy
redirect_to emails_path
end
def count_group_members(group)
Contactgroup.where(group_id: group).count
end
def group_member_name(group, i)
contact = Contactgroup.where(group_id: group).offset(i).pluck(:contact_id)
Contact.where(id: contact).pluck(:firstname)
end
private
def email_params
params.require(:email).permit(:subject, :message, :account_id, { contact_ids: [] }, { group_ids: [] })
end
end
问题在于,在 show.html.erb
视图中,用户会看到如下信息:
群组发送至:
经理 ["Gary", "Annie"] ["Annie"]
董事["Dave"]
当我想要的是显示为:
群组发送至:
经理加里,安妮
董事戴夫
有人可以帮我解决这个问题吗?
我需要做的就是更改控制器中的以下行:
def group_member_name(group, i)
contact = Contactgroup.where(group_id: group).offset(i).pluck(:contact_id)
Contact.find_by_id(contact).firstname
end
在我的 RoR 应用程序中,我试图在视图上显示来自关联表的信息。
我有这样的数据表:
Email
id subject
1 This week's work
2 Presentation
3 Spreadsheets
Recipients
email_id group_id
1 2
2 2
3 1
1 3
Groups
id name
1 Managers
2 Employees
3 Directors
Contactgroups
group_id contact_id
1 1
2 1
1 3
3 2
Contacts
id email
1 Gary
2 Dave
3 Annie
我想做的是显示收到电子邮件的组列表以及每个组成员。例如,电子邮件 1 "This week's work" 被发送到组 1 "Managers" 和 3 "Directors","Managers" 组由联系人 Gary 和 Annie 以及 "Directors" 组组成由联系人 "Dave" 组成。因此,在 show.html.erb
页面上,我想显示电子邮件的详细信息以及它被发送到的组和他们的每个联系人。
为了做到这一点,在 show.html.erb
页面上我有代码:
<p>
<strong>Groups Sent To:</strong></br>
<% @recipients.each do |recipient| %>
<%= recipient.group.name %>
<% for i in 0..count_group_members(recipient.group.id)-1 do %>
<%= group_member_name(recipient.group.id, i) %>
<% end %></br>
<% end %>
</p>
在我的 emails_controller
中有代码:
class EmailsController < ApplicationController
helper_method :count_group_members, :group_member_name
def index
@useraccounts = Useraccount.where(user_id: session[:user_id])
accountsarray = [0]
@useraccounts.each do |f|
accountsarray << f.account_id
end
# find all emails where the account_id is in the array - and so belongs to the user
@emails = Email.where(account_id: [accountsarray])
end
def show
@email = Email.find(params[:id])
@account = Account.find_by_id(@email.account_id)
@recipients = Recipient.where(email_id: @email.id)
end
def new
@email = Email.new
@email.recipients.build
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def edit
@email = Email.find(params[:id])
@useraccounts = Useraccount.where(user_id: session[:user_id])
end
def create
@email = Email.new(email_params)
if @email.save
redirect_to @email
else
render 'new'
end
end
def update
@email = Email.find(params[:id])
if @email.update(email_params)
redirect_to @email
else
render 'edit'
end
end
def destroy
@email = Email.find(params[:id])
@email.destroy
redirect_to emails_path
end
def count_group_members(group)
Contactgroup.where(group_id: group).count
end
def group_member_name(group, i)
contact = Contactgroup.where(group_id: group).offset(i).pluck(:contact_id)
Contact.where(id: contact).pluck(:firstname)
end
private
def email_params
params.require(:email).permit(:subject, :message, :account_id, { contact_ids: [] }, { group_ids: [] })
end
end
问题在于,在 show.html.erb
视图中,用户会看到如下信息:
群组发送至:
经理 ["Gary", "Annie"] ["Annie"]
董事["Dave"]
当我想要的是显示为:
群组发送至:
经理加里,安妮
董事戴夫
有人可以帮我解决这个问题吗?
我需要做的就是更改控制器中的以下行:
def group_member_name(group, i)
contact = Contactgroup.where(group_id: group).offset(i).pluck(:contact_id)
Contact.find_by_id(contact).firstname
end