mongoid has_many关系

时间:2011-07-07 09:10:55

标签: mongoid has-many

this is index.html.erb
            <p>
             <%= number_to_currency(product.price) %>  
             <%= button_to 'Add to Cart', cart_items_path(:product_id => product) %>
            </p>

cart.rb

class Cart
  include Mongoid::Document
  include Mongoid::Timestamps 
  # references_many :cart_items, :dependent => :destroy
   has_many :cart_items, :dependent => :destroy , :autosave => true 
  # embeds_many :cart_items

  # accepts_nested_attributes_for :cart_items
  # attr_accessible :cart_items_attributes
end

cart_item.rb

class CartItem
  include Mongoid::Document
  include Mongoid::Timestamps 
  # referenced_in :cart
  # embedded_in :cart
  belongs_to :cart
  belongs_to :product 
  # embedded_in :product  
  field :product_id, :type => Integer
  field :cart_id, :type => Integer
  field :quantity, :type => Integer
  field :price, :type => Float
end

application_controller.rb

...

私有

此应用中任意位置的当前购物车电话

def current_cart

cart = if session[:cart_id]
  Cart.find(session[:cart_id])
else
  Cart.create!
end
   session[:cart_id] = cart.id

cart_items.rb

... def create

@cart = current_cart 
product = Product.find(params[:product_id]) 
@cart_item = @cart.cart_items.build(:product => product)
# @cart_item = CartItem.new(params[:product => product])
 # @cart_item = CartItem.new(params[:cart_item])

respond_to do |format|
  if @cart_item.save
    format.html { redirect_to(@cart_item,  notice: 'Cart item was successfully created.') }
    format.json { render json: @cart_item, status: :created, location: @cart_item }
  else
    format.html { render action: "new" }
    format.json { render json: @cart_item.errors, status: :unprocessable_entity }
  end
end

端 ....

product.rb

require 'carrierwave/orm/mongoid'
class Product
  include Mongoid::Document
  include Mongoid::Timestamps 
  # include Mongoid::Observing
  # include Mongoid::Tree
  has_many :line_items

  # before_destroy :ensure_not_referenced_by_any_line_item

  field :title, :type => String
  field :description, :type => String
  # field :image, :type => String
  mount_uploader :image, ImageUploader
  field :price, :type => Float
  field :category_id, :type => Integer
  field :stock_level, :type => Integer


  set_callback(:destroy, :before) do |document|
    document.ensure_not_referenced_by_any_line_item
  end

  protected
  def ensure_not_referenced_by_any_line_item        
   if cart_items.empty? 
     return true 
   else
     errors.add(:base, 'Line Items present')
     return false 
    end
   end
end

结果如下: CartItemsController中的 NoMethodError #create BSON :: ObjectId('4e156c78421aa918eb000005')的未定义方法`cart_items':BSON :: ObjectId Rails.root:/ Users / user / railworks / rcart

应用程序跟踪|框架跟踪|完整追踪 app / controllers / cart_items_controller.rb:46:create' actionpack (3.1.0.rc4) lib/action_controller/metal/implicit_render.rb:4:in send_action' actionpack(3.1.0.rc4)lib / abstract_controller / base.rb:167:in process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/rendering.rb:10:in process_action' actionpack(3.1.0.rc4)lib / abstract_controller / callbacks.rb:18:in block in process_action' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:416:in 运行 _1145394839032970144__process_action_ 2623355606222143076 _callbacks' activesupport(3.1.0.rc4)lib / active_support / callbacks.rb:386:in _run_process_action_callbacks' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:in run_callbacks' actionpack(3.1.0.rc4)lib / abstract_controller / callbacks.rb:17:在process_action中的process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:30:in块中 activesupport(3.1.0.rc4)lib / active_support / notifications.rb:55:in block in instrument' activesupport (3.1.0.rc4) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport(3.1.0.rc4)lib / active_support / notifications.rb:55:in instrument' actionpack (3.1.0.rc4) lib/action_controller/metal/instrumentation.rb:29:in process_action' actionpack(3.1.0.rc4)lib / action_controller / metal / params_wrapper.rb:202:in process_action' actionpack (3.1.0.rc4) lib/action_controller/metal/rescue.rb:17:in process_action' activerecord(3.1.0.rc4)lib / active_record / railties / controller_runtime.rb:18:in process_action' actionpack (3.1.0.rc4) lib/abstract_controller/base.rb:121:in process' actionpack(3.1.0.rc4)lib / abstract_controller / rendering.rb:45:in process' actionpack (3.1.0.rc4) lib/action_controller/metal.rb:193:in dispatch' actionpack(3.1.0.rc4)lib / action_controller / metal / rack_delegation.rb:14:in dispatch' actionpack (3.1.0.rc4) lib/action_controller/metal.rb:236:in block in action' actionpack(3.1.0.rc4)lib / action_dispatch / routing / route_set.rb:65:in call' actionpack (3.1.0.rc4) lib/action_dispatch/routing/route_set.rb:65:in dispatch' actionpack(3.1.0.rc4)lib / action_dispatch / routing / route_set.rb:29:in call' rack-mount (0.8.1) lib/rack/mount/route_set.rb:152:in block in call' rack-mount(0.8.1)lib / rack / mount / code_generation.rb:93:in block in recognize' rack-mount (0.8.1) lib/rack/mount/code_generation.rb:75:in optimized_each' rack-mount(0.8.1)lib / rack / mount / code_generation.rb:92:in recognize' rack-mount (0.8.1) lib/rack/mount/route_set.rb:141:in call' actionpack(3.1.0.rc4)lib / action_dispatch / routing / route_set.rb:531:in call' warden (1.0.4) lib/warden/manager.rb:35:in block in call' warden(1.0.4)lib / warden / manager.rb:34:in catch' warden (1.0.4) lib/warden/manager.rb:34:in call' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / best_standards_support.rb:17:in call' rack (1.3.0) lib/rack/etag.rb:23:in call' rack(1.3.0)lib / rack / conditionalget.rb:35:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/head.rb:14:in call' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / params_parser.rb:21:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/flash.rb:243:in call' rack(1.3.0)lib / rack / session / abstract / id.rb:195:in context' rack (1.3.0) lib/rack/session/abstract/id.rb:190:in call' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / cookies.rb:321:in call' activerecord (3.1.0.rc4) lib/active_record/query_cache.rb:54:in call' activerecord(3.1.0.rc4)lib / active_record / connection_adapters / abstract / connection_pool.rb:448:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/callbacks.rb:29:in block in call' activesupport(3.1.0.rc4)lib / active_support / callbacks.rb:392:in _run_call_callbacks' activesupport (3.1.0.rc4) lib/active_support/callbacks.rb:81:in run_callbacks' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / callbacks.rb:28:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/reloader.rb:68:in call' rack(1.3.0)lib / rack / sendfile.rb:102:in call' actionpack (3.1.0.rc4) lib/action_dispatch/middleware/remote_ip.rb:48:in call' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / show_exceptions.rb:47:in call' railties (3.1.0.rc4) lib/rails/rack/logger.rb:13:in call' rack(1.3.0)lib / rack / methodoverride.rb:24:in call' rack (1.3.0) lib/rack/runtime.rb:17:in call' activesupport(3.1.0.rc4)lib / active_support / cache / strategy / local_cache.rb:72:in call' rack (1.3.0) lib/rack/lock.rb:34:in call' actionpack(3.1.0.rc4)lib / action_dispatch / middleware / static.rb:53:in call' railties (3.1.0.rc4) lib/rails/engine.rb:438:in call' railties(3.1.0.rc4)lib / rails / rack / content_length.rb:16:in call' railties (3.1.0.rc4) lib/rails/rack/log_tailer.rb:14:in call' rack(1.3.0)lib / rack / handler / webrick.rb:59:in service' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in service' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in块中的start_thread' 请求

参数:

{ “authenticity_token”=&gt; “中vwNaDytLihv5ev3M2tP5uZBjwR1 / t6ld0iFF1gGwWsw =”,  “PRODUCT_ID”=&gt; “中4e126c01421aa93347000002”} 显示会话转储

_csrf_token:“vwNaDytLihv5ev3M2tP5uZBjwR1 / t6ld0iFF1gGwWsw =” cart_id:BSON :: ObjectId('4e156c78421aa918eb000005') session_id:“532c1ca0f48a6fca905a97afba912e05”

我没有得到问题点。为什么会这样  未定义的方法`cart_items'???  感谢。

1 个答案:

答案 0 :(得分:0)

我需要查看抛出错误的代码,以便为您提供关于错误被抛出的明确答案(即create方法),但为什么不为此尝试购物车创建代码:

def current_cart
  cart = Cart.find_or_create_by(:id => session[:cart_id])
  session[:cart_id] ||= cart.id
  return cart
end

看起来你的当前实现在查找/创建它时实际上并没有返回购物车项目,而是返回session[:cart_id]变量。

另一种更有趣的解决方案可能是将帮助程序拆分为两种方法:

def current_cart_id
  session[:cart_id] ||= Cart.find_or_create_by(:id => session[:cart_id]).id
end

def current_cart
  @current_cart ||= Cart.find(current_cart_id)
end

然后,您可以随时轻松访问current_cart对象及其id