我一直在努力解决这个问题太久了,我希望你们中的一个人能够看到我不能做到的事情。它必须是一个简单的愚蠢错误,因为我在应用程序中的所有位置都没有错误。
问题:我有一个billing_type(引用)表,其中包含许多记录。我还有一个包含账单的账单表。当我在列表中显示开票表时,billing_type显示为:
"#<BillingType:0x00000006f49470>"
我假设是它的指针。
billing_type模型:
class BillingType < ActiveRecord::Base
validates :billing_type, presence: true, uniqueness: true
has_many :billings
accepts_nested_attributes_for :billings
end
结算模式:
class Billing < ActiveRecord::Base
belongs_to :billing_type
belongs_to :horse
validates :billing_type_id, presence: true
validates :horse_id, presence: true
end
架构:
create_table "billing_types", force: :cascade do |t|
t.string "billing_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "billing_types", ["billing_type"], name: "index_billing_types_on_billing_type", unique: true
create_table "billings", force: :cascade do |t|
t.date "billing_date"
t.integer "billing_type_id"
t.integer "horse_id"
t.string "notes"
t.decimal "cost"
t.date "date_billed"
t.date "date_received"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
控制器中的查询(注意:我导出了数据库并将其放入SQL中,它返回了所有内容,包括billing_type,o.k。):
def index
@billings = Billing.find_by_sql("SELECT billings.id, billings.billing_date, horses.horse_name, billings.billing_type_id, billing_types.billing_type, billings.notes, billings.cost, billings.date_billed, billings.date_received FROM billings JOIN horses ON billings.horse_id = horses.id JOIN billing_types ON billings.billing_type_id = billing_types.id ORDER BY billings.billing_date, LOWER(horses.horse_name)")
end
索引页面: 。 。
<div class = "table-container">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Billing date</th>
<th>Horse name</th>
<th>Billing type</th>
。 。
</tr>
</thead>
<tbody>
<% @billings.each do |billing| %>
<tr>
<% if billing.billing_date %>
<td><%= billing.billing_date.strftime("%d/%m/%Y") %></td>
<% else %>
<td><%= billing.billing_date %></td>
<% end %>
<td><%= billing.horse_name %></td>
<td><%= billing.billing_type %></td>
。 。 。
</tr>
<% end %>
</tbody>
</table>
提前感谢您提供任何帮助!
答案 0 :(得分:1)
这正是我期望看到的
billing.billing_type是整个BillingType类作为对象的表示。
如果您想要那么可能
billing.billing_type.inspect
正是你所期待的,但我怀疑你真正想要的是这个名称,在这种情况下你应该寻找显示对象的属性而不是对象本身。即
billing.billing_type.name