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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 如何在RubyonRails中使用where子句进行多态关联_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 如何在RubyonRails中使用where子句进行多态关联

Ruby on rails 如何在RubyonRails中使用where子句进行多态关联,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,这就是我正在做的 class Temp < ActiveRecord::Base belongs_to :tempable,polymorphic: true end class TempAgain < ActiveRecord::Base has_many :temps , as: :temable end Temp.includes(:tempable).where("{temable.table_name}.text = ?",value) 我认为你需要学习多态关联。

这就是我正在做的

class Temp < ActiveRecord::Base
 belongs_to :tempable,polymorphic: true
end
class TempAgain < ActiveRecord::Base
 has_many :temps  , as: :temable
end

Temp.includes(:tempable).where("{temable.table_name}.text = ?",value)

我认为你需要学习多态关联。这就是一个例子

型号:

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
  belongs_to :article, ->(record) { where(comments: { commentable_type: "Article" }) }, foreign_key: :commentable_id, class_name: "Article"
end

class Article < ActiveRecord::Base
  has_many :comments, as: :commentable
end
撬:


请试一试。

那么问题是什么?它不起作用,取消定义表名temable我也试了一下。包括:tempable.wheretempable:{text:text}它也不起作用。请把问题放在问题的主体中。temable还是tempable?确保它在任何地方都是一致的,我可以这样做,文章标题是标题注释的所有注释。包括:commentable.wherecommentable.title=,title好的,尝试添加belides\u to:article、->record{wherecomments:{commentable\u type:article},foreign\u key::commentable\u id,class\u name:article to Comment模型,您可以执行Comment.joins:article.mergeArticle.where'articles.title LIKE?','%xxx%'。all.yes我可以在从文章查询时访问数据,但这也很有帮助
[0] pry(main)> article = Article.create(title: 'xxx', content: 'xxx')
[1] pry(main)> 5.times.map { article.comments.build(content: 'xxx').save }
[2] pry(main)> Article.includes(:comments).find(1).comments
  Article Load (0.2ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1  [["id", 1]]
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_type" = 'Article' AND "comments"."commentable_id" IN (1)
=> [#<Comment:0x007fa0e22fff50
  id: 1,
  content: "comment_0",
  commentable_id: 1,
  commentable_type: "Article",
  created_at: Fri, 22 Jan 2016 10:55:02 UTC +00:00,
  updated_at: Fri, 22 Jan 2016 10:55:02 UTC +00:00>]
[3] pry(main)> Comment.joins(:article).merge(Article.where('articles.title LIKE ?', '%xxx%')).all
=> [#<Comment:0x007fc8b2be87a0
  id: 1,
  content: "comment_0",
  commentable_id: 1,
  commentable_type: "Article",
  created_at: Fri, 22 Jan 2016 10:55:02 UTC +00:00,
  updated_at: Fri, 22 Jan 2016 10:55:02 UTC +00:00>]