Rails:活动记录协会
现在,我对抗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
如果您有一对多关系,请不要使用has_and_belongs_to_many
。创建和维护另一个表会增加太多的复杂性。我的建议是找出为什么你不能使一对多关系工作。
你应该能够使这项工作:
class User < ActiveRecord::Base
has_many :products #Plural
end
class Product < ActiveRecord::Base
belongs_to :user #Singular
end
如果它不能正常工作,复制/粘贴您的控制器,您使用一个一对多的关系码!
祝你好运
因为Product
有很多users
, p.users
是一个类似数组的对象。你需要p.users[0].username
。
但是,为什么你不能使它成为一个标准的一对多关联?
我以某种方式没有设法得到保存在产品中的用户变量。当我设置一对多关系时。我收到一条错误消息: p = Product.last p.user NoMethodError:未定义的方法'user'for# – 2012-07-27 13:33:54
我已经用products_controller.rb更新了我的帖子。 我尝试了你的想法,但不幸的是我得到了IRB同样的错误: p.user NoMethodError:未定义的方法'用户为# 我能找到一个用户的所有产品,但不是用户一个产品。 – 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