Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails在导入的文件上添加外键_Ruby On Rails_Session_Csv_Model_Import - Fatal编程技术网

Ruby on rails rails在导入的文件上添加外键

Ruby on rails rails在导入的文件上添加外键,ruby-on-rails,session,csv,model,import,Ruby On Rails,Session,Csv,Model,Import,我正在尝试在导入的列表上设置外键。我的想法是创建一个before_save回调来使用会话变量更新外键。然而,我的理解是,您不能或者不应该从模型中访问会话变量。如果是这样的话,我还有什么选择呢。我的代码如下: 在模型中: class Contact < ActiveRecord::Base attr_accessible :first_name, :last_name, :email, :mobile, :restaurant _id belongs_to :restaurant

我正在尝试在导入的列表上设置外键。我的想法是创建一个before_save回调来使用会话变量更新外键。然而,我的理解是,您不能或者不应该从模型中访问会话变量。如果是这样的话,我还有什么选择呢。我的代码如下:

在模型中:

class Contact < ActiveRecord::Base
   attr_accessible :first_name, :last_name, :email, :mobile, :restaurant _id
   belongs_to :restaurant

    before_save :add_restaurant

    def add_restaurant
     contact = Contact.find_by_restaurant_id(session[:current_restaurant])
     contact.restaurant_id = (session[:current_restaurant])
    end

  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
    contact = find_by_first_name_and_last_name(row["first_name"], row["last_name"]) || new
    contact.attributes = row.to_hash.slice(*accessible_attributes)
    contact.save!
    end
  end
end

您可以先找到餐厅,然后将联系人添加到餐厅联系人中

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    contact = find_by_first_name_and_last_name(row["first_name"], row["last_name"]) || new
    contact.attributes = row.to_hash.slice(*accessible_attributes)
    restaurant = Restaurant.find_by_id(r_id)    # Get the restaurant here
    restaurant.contacts << contact              # This will do a save immediately
  end
end
def self.import(文件)
CSV.foreach(file.path,headers:true)do |行|
contact=按姓氏查找(第[“姓氏”]行,第[“姓氏”]行),新建
contact.attributes=行到散列切片(*可访问的属性)
餐厅=餐厅。按id(r_id)查找餐厅#在这里找到餐厅

restaurant.contacts在restaurant和上面的_id之间真的有空格吗?我还没有找到解决这个问题的方法,我也不知道从上面的代码可以到哪里去。非常感谢您的帮助。这个问题在6个月后得到了更彻底的回答,[这里][1][1]:这对我来说不起作用,但我有一个愚蠢的问题。用户登录时,餐厅id存储在会话变量中。当他们上传文件时,CSV中的列是first_name、last_name、email和mobile,没有提及餐厅id(因为用户不会知道,我也不想让他们知道)。restaurant=restaurant.find_by_id(r_id)如何在不访问会话变量的情况下查找与当前用户关联的restaurant_id。啊-我明白了。会话在控制器中可用,您需要将其传递给import\u file方法。因此,我会将其切换到控制器中的餐厅,然后调用restaurant.import_contacts(文件)并将其放入餐厅模型中。
def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    contact = find_by_first_name_and_last_name(row["first_name"], row["last_name"]) || new
    contact.attributes = row.to_hash.slice(*accessible_attributes)
    restaurant = Restaurant.find_by_id(r_id)    # Get the restaurant here
    restaurant.contacts << contact              # This will do a save immediately
  end
end