Rails控制台复杂查询

时间:2016-03-04 22:47:43

标签: ruby-on-rails ruby ruby-on-rails-3 postgresql rails-activerecord

我想使用后续查询发布子类别,如下所示:

Gender Model:

class Gender < ActiveRecord::Base
  extend FriendlyId
  friendly_id :gender, use: [:slugged, :history]

  has_many :categories
  has_many :subcategories, through: :categories
  has_many :products
  accepts_nested_attributes_for :categories
  accepts_nested_attributes_for :products
  accepts_nested_attributes_for :subcategories
  attr_accessible :gender, :categories_attributes, :subcategories_attributes,


end

但显示此错误!

  耙子流产了! NoMethodError:未定义的方法`子类别&#39;对于   分类

我搜索但无法找到如何使用链式查询创建数据。

static void main(){

if (Environment.UserInteractive)
            {

                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1";
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    //di.Delete(); 
                }




                StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1\\program.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " : Here if\n");
                //     sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : ");
                //    sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : ");
                sw.Flush();
                sw.Close();

                Console.WriteLine("here1");
                Service1 service1 = new Service1();
                string[] args = { "Kun", "Singh" };
                service1.TestStartupAndStop(args);
            }
            else
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1";
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    //di.Delete(); 
                }




                StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1\\program.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " : Here else\n");
           //     sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : ");
            //    sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : ");
                sw.Flush();
                sw.Close();





                Console.WriteLine("here2");
                // Put the body of your old Main method here.
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new Service1()
                };
                ServiceBase.Run(ServicesToRun);


                // RunAsync().Wait();
                Service1 myServ = new Service1();
                myServ.write("hello bb");
                Console.Read();
            }

1 个答案:

答案 0 :(得分:3)

where(name: 'clothes')返回查询,而不是模型实例。假设您的类别中有has_many :subcategories,且类别名称是唯一的,因此您可以使用find_by查找服装类别,然后在其上调用subcategories。同样,为了查找性别:

Gender.find_by(gender: 'Masc')  # This gives you a single Gender instance
      .categories
      .find_by(name: 'clothes') # and then a single Category here
      .subcategories
      .create(name: 't-shirt')
相关问题