我有 3 个模型:User、Order 和 UserOrder
用户订单.rb
belongs_to :user, :class_name => "User"
belongs_to :order, :class_name => "Order"
用户.rb
has_many :user_orders, foreign_key: :user_id,
dependent: :destroy
has_many :ordering, through: :user_orders,
source: :order
订单.rb
has_many :reverse_user_orders, foreign_key: :order_id,
dependent: :destroy,
class_name: "UserOrder"
has_many :orderers, through: :reverse_user_orders, source: :user
我在orders_controller.rb中有控制器操作,它创建了用户和订单之间的关系并且它工作正常(它在数据库中创建记录)
def create
...
if @order.save
current_user.order!(@order)
....
我想在订单的 index.html.erb文件中呈现 user.name
<%= order.orderers.name %>
但在 HTML 中,它只呈现标题“用户”。如果我使用 <%= order.orderers %> 它会返回完整的哈希值,包括 user.name。
[#<User id: 19, name: "Peter Jackson", email: "foobar@vk.com", remember_token: "pj6eI2mc2_6G0UbumoEqMA", password_reset_token: nil, password_reset_sent_at: nil, created_at: "2013-01-27 14:10:15", updated_at: "2013-01-27 14:10:28", provider: nil, uid: nil, passport: nil, region: "Zimbabve"]
有什么方法可以在订单的 index.html.erb 中呈现 user.name?谢谢