我有以下几点:Ruby on Rails的 - 连接两个表
模式
create_table "mouldings", :force => true do |t|
t.string "suppliers_code"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.integer "supplier_id"
t.decimal "length"
t.decimal "cost"
t.decimal "width"
t.decimal "depth"
end
create_table "suppliers", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
模式
class Moulding < ActiveRecord::Base
# Set up associations
belongs_to :suppliers
end
class Supplier < ActiveRecord::Base
# Associations
has_many :mouldings, :dependent => :destroy
end
控制器
def show
@moulding = Moulding.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @moulding }
end
end
我想加入这两个表,因此我可以在视图中包含供应商的名称。控制器的正确语法是什么?
你可能想提到Ruby不仅仅是标签。标题和正文可能有用。 – 2010-11-08 17:27:45