Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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/Rails活动记录中将两个表连接在一起_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 在Ruby/Rails活动记录中将两个表连接在一起

Ruby on rails 在Ruby/Rails活动记录中将两个表连接在一起,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我是Rails新手,不知道如何做到这一点: 我正在编写一个rake脚本,需要从两个表中引用一个值: 动物表中的所有者id引用所有者表: Animals:[ {id:1, name:"Bow wow", owner_id:1, score:null}, {id:2, name:"Chiaow", owner_id:2, score:null}, {id:3, name:"Fishbob and Ben", owner_id:9, score:null} ] Owner:[ {id:

我是Rails新手,不知道如何做到这一点:

我正在编写一个rake脚本,需要从两个表中引用一个值: 动物表中的所有者id引用所有者表:

Animals:[
 {id:1, name:"Bow wow", owner_id:1, score:null}, 
 {id:2, name:"Chiaow", owner_id:2, score:null}, 
 {id:3, name:"Fishbob and Ben", owner_id:9, score:null}
]

Owner:[
  {id:1, name:"Doug"}, 
  {id:2, name:"Michelle"}, 
  {id:9, name:"Ben"}
]
我想将这两个表合并,得到如下结果:

Combined = [
  {id:1, score:null, keywords:"Bow Wow Doug", name:"Bow wow", owner:"Doug"}, 
  {id:2, score:null, keywords:"Chiaow Michelle", name:"Chiaow, owner:Michelle",
  {id:3, score:null, keywords:"Fishbob and Ben", name:"Fishbob", owner:"Ben"}
]
我还想通过索引关键字使用

  combined_list = Combined.where("keywords LIKE (?)", "%#{chosen_word}%")

这是可能的吗?

如果您将两个模型“动物”和“所有者”联接在一起,则可以从联接表中选择要在实例中使用的属性:

combined=Animal.joins:owner.select:id,:name,'owner.name作为所有者\u name' 放置combined.first.name,combined.first.owner\u name 这当然意味着动物模型属于:所有者

要筛选此关系,我将执行以下操作:

cw=%{selected_word}% 组合。哪里有“动物。名字像?”,cw。 或组合。哪里有“owner.name LIKE?”,cw
我想应该是owners.name combined=Animal.joins:owner.select:id,:name,'owners.name as owner\u name'