Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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 关联:与现有表的一对多关联_Ruby On Rails_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 关联:与现有表的一对多关联

Ruby on rails 关联:与现有表的一对多关联,ruby-on-rails,activerecord,ruby-on-rails-4,Ruby On Rails,Activerecord,Ruby On Rails 4,Rails初学者: 我已经有了一个数据库和表,所以命名约定让我有些头疼 class Item < ActiveRecord::Base belongs_to :categorie, :foreign_key => "catid" end class Categorie < ActiveRecord ... has_many :item end 你能行 Item.create(:catid=>@categorie.id) @categorie=c

Rails初学者:

我已经有了一个数据库和表,所以命名约定让我有些头疼

class Item < ActiveRecord::Base    
  belongs_to :categorie, :foreign_key => "catid"   
end

class Categorie < ActiveRecord ...    
  has_many :item
end
你能行 Item.create(:catid=>@categorie.id)

@categorie=categorie.find(params[:id])或带有categorie.all
放置每个循环并找到id。

首先,您应该为模型使用类别,因为rails智能地理解多个类别或表格

第二,你应该有这样的东西

class Item < ActiveRecord::Base    
  belongs_to :Category, :foreign_key => "catid"   
end

class Categorie < ActiveRecord ...    
  has_many :items
end

i = Item.first



c = i.Category



c.items #to find all items that belong to the category c
class项“catid”
结束
类分类
您是rails初学者,但可能不是程序员初学者,因此我将深入讲解一下类

只是一个保存方法的数据对象。没别的了。这里有一个简单的方法:

class Cow
    def talk
        "moo"
    end
end
Cow
是类,
talk
是方法。现在,如果我们在内存中有上述类,我们就不能在控制台中执行此操作:

talk
因为该方法在全局范围内不可用。这是一件好事,因为这可能会导致bug,而且效率低下。想象一下,如果我们有一些动物:

class Cat
    def talk
        "meow"
    end
end

class Dog
    def talk
        "woof"
    end
end
运行
talk
,计算机如何知道要运行哪个
talk
?相反,我们调用类内部的方法,如下所示:

Cow.talk #=> "moo"
Cat.talk #=> "meow"
Dog.talk #=> "woof"
class Item < ActiveRecord::Base
    def first
       # code is in here that queries the table in your database that has
       # the downcased and pluralized name of Item (so items) and returns the first
       # row of that table
    end 

    # down here is all of your methods you've probably created. Validations and the like.
end
希望现在,这段代码:

Item.first
不那么神秘
Item
是一个类,
first
是该类中可用的方法

现在我知道
Item
是一个模型,但在rails中,模型只是继承了ActiveRecord中一系列有用方法的类。在
项的顶部
模型中,您应该看到:

class Item < ActiveRecord::Base
首先
,而不是像我的示例中那样返回字符串,这样做更有用;它在数据库中查询具有其类的downcased和复数名称的表。因此
Item.first
查询
items
表,并返回第一行

现在,我必须诚实地说,不管你怎么说,我发现非常怀疑
I.Categorie
是否根据I的“catid”找到了正确的分类。如果真的是这样,我觉得你已经做了一些疯狂的工作来让它工作。这就是应该发生的事情:

i.Categorie
NoMethodError: undefined method `Categorie' for #<Item:0x00000005905830>
这是有道理的,因为我在这里看不到“分类”方法:

class Item < ActiveRecord::Base
    def first
       # code is in here that queries the table in your database that has
       # the downcased and pluralized name of Item (so items) and returns the first
       # row of that table
    end 

    # down here is all of your methods you've probably created. Validations and the like.
end
希望你对现在发生的事情有更多的了解。如果你想让你的代码工作,你应该这样做。仔细看,有几个细微差别:

i = Item.first    # i is set to the first instance of Item
c = i.categorie   # c is set to the instance of Categorie that i belongs to
is = c.items      # returns an array consisting of all the Item instances that belong to the Categorie instance in c
我们也可以这样做:

is.first # returns i
那么,这些简便的方法是从哪里来的呢?
i
中的
category
方法(
i.category
),
c
中的
方法(
c.items
)?答案是,它们是由Rails基于您的继承动态创建的,并通过 “基于您的继承”是指您如何使用继承方法,
属于
有很多

class Item < ActiveRecord::Base    
  belongs_to :categorie, :foreign_key => "catid" # creates categorie method that returns the instance of Categorie this instance of Item belongs to    
end

class Categorie < ActiveRecord ...    
  has_many :item # creates items method that returns an array of all the instances of Item that belong to this instance of Categorie  
end
class项“catid”#创建categile方法,该方法返回该项实例所属的categile实例
结束
类分类

我还要指出,category是一个非常糟糕的模型名称,纯粹是因为它拼写错误。也许
类型
会更好?

对于
有很多关系,你应该使用复数形式。将其更改为
有很多:项目
,然后尝试
c.items
thx Pavan和Kirti,查看答案和我的评论below@MrYoshiji啊,谢谢,我想我的x)c项目负载有点大(14.5毫秒)选择
items
*FROM
items
WHERE
items
categorie\u id
=20 Mysql2::错误:未知列“items.categorie\u id”在“WHERE子句”中:选择
items
items
其中
items
categorie\u id>=20 ActiveRecord::statement无效:Mysql2::错误:未知“where子句”中的“items.categorie_id”列:从
items
where
items
categorie_id
=20中选择
items
*,如果是这样,则categorie_id不存在,存在的是catid。我可以简单地将该列从catid重命名为catile\u id,但这不是pointi=Item。第一个Item Load(13.2ms)选择
items
from
items
ORDER BY
items
id
ASC LIMIT 1=>。#正如我所说,数据库是预先存在的c=I从
categories
中选择
categories
其中
categories
=20 LIMIT 1=>中选择
categories
*
i = Item.first    # i is set to the first instance of Item
c = i.categorie   # c is set to the instance of Categorie that i belongs to
is = c.items      # returns an array consisting of all the Item instances that belong to the Categorie instance in c
is.first # returns i
class Item < ActiveRecord::Base    
  belongs_to :categorie, :foreign_key => "catid" # creates categorie method that returns the instance of Categorie this instance of Item belongs to    
end

class Categorie < ActiveRecord ...    
  has_many :item # creates items method that returns an array of all the instances of Item that belong to this instance of Categorie  
end