Railsの質問...Rails Activerecordが値を返さない
ビュー内でデータベースに結合されたいくつかのテーブルからデータを出力しようとしています。具体的には、私は、各プロパティのアドレス(プロパティモデルから「アドレス」)をプリントアウトしたい、それが
質問に対応するコメント(金利モデルから「COMMENT_TEXT」)です。今私はテーブルを結合してい
1)景色;しかし、私はこれが正しいとは思っていませんが、コントローラーに参加させる方法は不明です。私はビューの中のテーブルにまったく参加する必要がありますか?それともコントローラーでこれが起こっているのでしょうか?
2)私の見解では、アドレスとそれに対応するcomment_textを印刷したいと思います。ただし、今すぐ印刷しようとすると、アクティブなレコード関係オブジェクトのように見えます。「["test"] "」と表示され、「test」と表示されます。
アイデア?以下のコード:
スキーマ:
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