現在,我對抗AR問題已有兩天了。我知道有一個簡單的解決方案,但我無法弄清楚。Rails:活動記錄協會
我有兩個模型產品和用戶,應該鏈接在一起。產品應該屬於用戶,用戶應該有許多產品。我沒有設法在我的控制器中設置用戶變量,所以我去了一個habtm關聯。下面是型號:
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
#...
has_and_belongs_to_many :products
Product.rb
class Product < ActiveRecord::Base
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :allow_destroy => :true
has_and_belongs_to_many :users
products_controller.rb
class ProductsController < ApplicationController
def index
@products = Product.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
def new
@user = current_user
@product = Product.new#@user.products.build(params[:product])
@product.product_images.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
def create
@user = current_user
@product = @user.products.build(params[:product])#Product.new(params[:product])
@product.product_images.build
@user.products << @product
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created, location: @product }
else
format.html { render action: "new" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<%= form_for @product, :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>
schema.rb
....
create_table "products_users", :id => false, :force => true do |t|
t.integer "product_id"
t.integer "user_id"
end
....
所以,即使我知道,應該是用戶和產品之間的一個一對多的關聯,這工作我出去。 在IRB,我可以這樣做:用的habtm協會
u = User.first
u.products
我甚至可以做到這一點viseversa:
p = Product.first
p.users
但我不能訪問用戶屬性:
p.users.username:
NoMethodError: User Load (2.0ms) SELECT "users".* FROM "users" INNER JOIN "products_users" ON "users"."id" = "products_users"."user_id" WHERE "products_users"."product_id" = 12
有人能幫我解決這個問題嗎?我究竟做錯了什麼?
非常感謝!你的幫助可以節省我的週末!
編輯:這是我的一個一對多的關係控制器動作:
class ProductsController < ApplicationController
helper_method :sort_column, :sort_direction
def index
@products = Product.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
末
高清顯示 @product = Product.find(PARAMS [:編號]) @images = @ product.product_images respond_to do | format | format.html#show.html.erb format.json {渲染JSON:@product} 結束 結束
def new
@user = current_user
@product = Product.new#@user.products.build(params[:product])
@product.product_images.build
#3.times { @product.product_images.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
def edit
@product = Product.find(params[:id])
@product.product_images.build
end
def create
@user = current_user
@product = @user.products.build(params[:product])#Product.new(params[:product])
@product.product_images.build
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created, location: @product }
else
format.html { render action: "new" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
# params[:sort] || "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
# params[:direction] || "asc"
end
end
EDIT2:Schema.rb
create_table "products", :force => true do |t|
t.string "title"
t.string "description"
t.string "testimonial"
t.decimal "credits", :precision => 3, :scale => 0
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.decimal "age"
t.string "condition"
t.string "product_image"
t.string "image"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
t.integer "product_id"
end
我已經用products_controller.rb更新了我的帖子。 我嘗試了你的想法,但不幸的是我得到了IRB同樣的錯誤: p.user NoMethodError:未定義的方法'用戶爲#<產品:0x007f82ba7a7468> 我能找到一個用戶的所有產品,但不是用戶一個產品。 – 2012-07-27 14:59:28
你能給我你創建的數據庫模式嗎?您應該在表產品 – 2012-07-27 15:02:32
中有一個名爲user_id的字段,我使用用戶和產品表的架構編輯了我的問題。我在產品表中有user_id。我甚至將product_id設置爲用戶表。但不知何故,它只適用於user.products,而不是product.users 謝謝你看看,安東尼! – 2012-07-27 15:12:52