2

I have a model Order. In the index, I want the current_user to see the index with only their orders. Order belongs_to :admin_user. AdminUser has_many :orders. I am using activeadmin in my app, if that makes a difference. I am getting this error:

Couldn't find Order without an ID

The line giving the error is the second line in my order controller.(redacted unnecessary info)

index do 
    @order = Order.where(admin_user_id: current_admin_user.id, order_id: resource.id)
     column "ID" do |order|
      link_to order.id, admin_order_path(order)
     end
    column "Proof" do |order|
     image_tag order.proof_url(:proof).to_s
        end
        column "Name" do |order|
      link_to order.name, admin_order_path(order)
    end
    column(:customer, :sortable => :customer_id)
        column "Category", :order_category
        column "Status", :order_status
        column "Priority", :order_priority 
    column "Due Date", :end_date
    default_actions
  end

here is my order model requested by @jamesw

class Order < ActiveRecord::Base
  attr_accessible :color_back, :color_front, :color_sleeve, :end_date, :name, :start_date, :whiteboard, :customer_id, :order_category_id, :order_type_id, :order_status_id, :order_priority_id, :print_location_id, :artwork, :proof, :line_items_attributes, :assignee_id, :admin_user_id

  mount_uploader :artwork, ArtworkUploader
  mount_uploader :proof, ProofUploader


  has_many :line_items
  belongs_to :assignee, :class_name => "AdminUser"
  belongs_to :customer
  belongs_to :order_category
  belongs_to :order_type
  belongs_to :order_status
  belongs_to :order_priority
  belongs_to :print_location
  belongs_to :admin_user
  accepts_nested_attributes_for :line_items, :allow_destroy => true

  scope :owned_by, lambda { |user| includes(:assignee).where("admin_users.id = ?", user.id) }



  def default_values
    if new_record?
      self.start_date ||= Date.today
      self.number ||= (Order.maximum(:number) + 1 rescue 1)
    end
  end
end
DhatchXIX
  • 437
  • 1
  • 3
  • 17
  • That approach seems completely wrong. You seem to be trying to get a single order yet you state you want all orders for the current admin user so why not just get all the orders for the logged in admin? '@orders = current_admin.orders' – jamesc May 21 '13 at 00:53
  • It doesnt give me an error to do this but nothing has changed. Different users still see other users orders – DhatchXIX May 22 '13 at 00:48
  • Using the code I gave you, it is not possible to see orders for all users which means there is code running that you have not shown us. Please provide the controller index action and the index.html.erb template plus log file entries for the index action – jamesc May 22 '13 at 06:52
  • Also run a rails console session, find the first order `o = Order.first` then check that the admin user exists for that order `o.admin_user.inspect` or whatever the name of the admin_user model relationship is. Could you also post your order model code – jamesc May 22 '13 at 06:55
  • @jamesw This is all of the code for the index, Inherited resources is creating the view from here so no indext.html.erb. I am suspecting that the real issue here is that my orders db isnt showing a admin_user_id for each order, so i am going to go back and look into this first. – DhatchXIX May 22 '13 at 13:00
  • If your orders records have no id assigned for the admin_user then you wouldn;t see any orders at all when you use `current_admin.orders` – jamesc May 22 '13 at 13:19
  • `belongs_to :admin_user_id` seems odd, that looks more like a column name rather than a table name? – jamesc May 22 '13 at 16:50
  • That is my fault, i was testing something and left that there, i edited it back to belongs_to :admin_user. – DhatchXIX May 22 '13 at 17:33

3 Answers3

0

It looks like you're trying to filter the Orders table by order_id. Unless you've built your DB in a non-standard manner, the ID field of the orders table would typically be id (not order_id).

That issue aside, I doubt you want to be passing in an order id for the index action since that would only return a single record, and by it's nature the index action should list many records (in your case, all records for the current_admin_user).

If neither of those issues solve your problem, try commenting out the lines 1 by 1.

jshkol
  • 1,777
  • 14
  • 19
0

Try to add this controller method

ActiveAdmin.register Order do
  controller do
    def scoped_collection
     Order.where(:admin_user => current_admin_user.id)
    end
  end
end

see more here: Two pages for the same resource - ActiveAdmin

Community
  • 1
  • 1
James
  • 744
  • 1
  • 4
  • 10
0

I fixed the issue by taking the line in question out and using scope_to :current_user. I am wondering though, how to add a conditional statement to still allow the admin to view this? here is a look at the controller now.

scope_to current_user

index do 
     column "ID" do |order|
      link_to order.id, admin_order_path(order)
     end
    column "Proof" do |order|
     image_tag order.proof_url(:proof).to_s
        end
        column "Name" do |order|
      link_to order.name, admin_order_path(order)
    end
    column(:customer, :sortable => :customer_id)
        column "Category", :order_category
        column "Status", :order_status
        column "Priority", :order_priority 
    column "Due Date", :end_date
    default_actions
  end
DhatchXIX
  • 437
  • 1
  • 3
  • 17