ActiveRecord通过多个连接查询

时间:2016-09-19 16:56:00

标签: sql ruby-on-rails ruby join activerecord

以下是我的模特:

post.rb

class Tagging < ActiveRecord::Base
  belongs_to :post
  belongs_to :tag, counter_cache: :posts_count
end

tagging.rb

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :posts, through: :taggings
end

tag.rb

class Collectable < ActiveRecord::Base
  belongs_to :post
  belongs_to :collection, counter_cache: :posts_count
end

collectable.rb

class Collection < ActiveRecord::Base
  has_many :collectables
  has_many :posts
end

collection.rb

tag.name

一个集合有很多帖子,一个帖子有很多标签。现在我正在尝试创建一个搜索栏,用于搜索具有特定标签def self.search(search) Collection.joins(:posts) ... ? end 是搜索词)的帖子的收藏集。例如,Collection 1有一个标签为#cat的帖子。现在,如果用户搜索“cat”,则Collection 1将显示在结果中。我不确定查询应该如何实现这一目标。

var total_cost = 10000;

2 个答案:

答案 0 :(得分:1)

试试这个:

29% OK
First | Cocoa Apples are
23% NOT
Second | Test Text
18% NOT
Third | Mango Alpa Tango
16% NOT
Fourth | Apples are
33% OK
Fifth | Text Testing App
10% NOT
Sixth | Apples are Gold Duck
28% OK
Seventh | Alpa Apples are Tango
66% OK
Eighth | Oh My Apples are Avocado
20% NOT
Ninth | This Is My Text
25% NOT
Tenth | This Is Hard

答案 1 :(得分:-1)

试试这个

#content-product.php start.... 

do_action( 'woocommerce_shop_loop_item_title' );

VALUES HERE SOMEHOW?


/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

#rest of the content-product.php...

您可以使用includes,eager_load和join,但是当您使用联接时,只有它会收集那些在所有其他情况下拥有帖子的收藏,它将收集所有收集帖子且没有帖子的收藏。