Rails的問題...Rails的Activerecord不返回值
我想從我的數據庫在視圖中連接在一起的幾個表中打印數據。具體而言,我想打印出各個屬性地址(房產模型「地址」),它的相應的註釋(從利益型「COMMENT_TEXT」)
問題:
1)現在我加盟的表風景;不過,我認爲這是不正確的,但我不確定如何將它們加入控制器。我應該加入視圖內的表嗎?或者應該在控制器中發生這種情況?
2)在我的視圖中,我想打印出地址和相應的comment_text;然而,當我嘗試打印出現在,它看起來像一個活躍的記錄關係對象,即顯示「[」測試「]」,我希望它顯示「測試」
任何想法?下面的代碼:
模式:
create_table "interests", force: :cascade do |t|
t.integer "user_id"
t.integer "property_id"
t.string "interested", default: "unknown"
t.string "comment_text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "interests", ["property_id"], name: "index_interests_on_property_id"
add_index "interests", ["user_id"], name: "index_interests_on_user_id"
create_table "properties", force: :cascade do |t|
t.string "address"
t.string "city"
t.string "state"
t.integer "zip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
型號:
class Property < ActiveRecord::Base
has_many :interests
has_many :users, through: :interests
default_scope {order("id ASC")}
end
class Interest < ActiveRecord::Base
belongs_to :property
belongs_to :user
validates :user_id, :presence => true
validates :property_id, :presence => true
end
控制器:
def index
@properties = Property.all
end
查看:
<% @properties.each do |p| %>
<td><%= p.interests.collect(&:comment_text) %>
<% end %>
感謝slowjack!不知道我是如何錯過的,但工作! – Anthony