我有问题。我想从wallet_id电子邮件构件,这种关系不是直接给成员,而不是投资者的帐户先像下面的示例模型。
member.rb
class Member < ActiveRecord::Base
has_one :investor_account, dependent: :destroy
end
investor_account.rb
class InvestorAccount < ActiveRecord::Base
belongs_to :member
has_many :wallets, dependent: :destroy
end
wallet.rb
class Wallet < ActiveRecord::Base
belongs_to :investor_account
end
top_up.rb
belongs_to :wallet
/top_UPS/_form.HTML.slim
= simple_form_for [:transaction, @top_up] do |f|
.form-group
= f.input :wallet_id, collection: @wallet, input_html: { class: 'form-control' }, include_blank: "Select..."
.form-group
= f.input :amount, input_html: { min: 0, value: 0 }
/controllers/top_UPS_controller.日本
def new
@top_up = TopUp.new
@wallet = Wallet.joins(:investor_account).where(investor_accounts: {approval_status: 'approved'})
end
上的数据“f.input:wallet_id ......”被露面,但它不是作为成员,而不是它显示在所有钱包下拉#<Wallet:0x007fd6d795e808>
的电子邮件,以前我也写代码像下面。
= f.input :wallet_id, collection: @wallet, :id, :email, input_html: { class: 'form-control' }, include_blank: "Select..."
但它抛出的问题没有发现的电子邮件。我的问题是如何传递的成员对@wallet = ...
变量获得的电子邮件显示会员我的形式?有没有更好的办法来获取?
您可以使用label_method
和value_method
PARAMS(docs):
= f.input :wallet_id, collection: @wallet, value_method: :id, label_method: :email, input_html: { class: 'form-control' }, include_blank: "Select..."
此外,如果你只需要ID和电子邮件,也没有必要来从该数据库中的所有其他数据,你可以使用pluck
:
# controller
def new
@top_up = TopUp.new
@wallet = Wallet.joins(:investor_account).where(investor_accounts: {approval_status: 'approved'}).pluck(:id, :email)
end
# form
= f.input :wallet_id, collection: @wallet, value_method: :first, label_method: :last, input_html: { class: 'form-control' }, include_blank: "Select..."