Open regdog opened 13 years ago
Can you supply the SQL that MetaSearch generates? Also, is this Rails 3.0 or 3.1?
Sent from my iPhone
On Sep 4, 2011, at 12:16 AM, regdog reply@reply.github.com wrote:
I have a complex model design.
Say, the Tag model here uses single-table inheritance for different type of categories. The Tagging model use polymorphic assocation for different taggable type of tagging as well.
Given the following models:
class Tag < ActiveRecord::Base end
class PostCategory < Tag has_many :taggings, :foreign_key => "tag_id" has_many :posts, :through => :taggings end
class Tagging < ActiveRecord::Base belongs_to :taggable, :polymorphic => true
belongs_to :post, :foreign_key => "taggable_id" belongs_to :post_category, :foreign_key => "tag_id" end
class Post < ActiveRecord::Base has_many :taggings, :as => :taggable has_many :categories, :through => :taggings, :source => :post_category end
I want to find Posts which category name may contain string like "eat". e.g. @search = Post.search({:categories_name_contains=>"eat"})
It will return all the posts or none of the posts in database table, depending on whether the search string is to be contained in some post category name.
Is anything wrong with the way I named the search option or is this a Meta-Search limitation ?
Thanks Meta Search! It's a great gem!
Cameo
Reply to this email directly or view it on GitHub: https://github.com/ernie/meta_search/issues/59
I am on Rails 3.0 9. The SQL is as following:
SELECT posts
.* FROM posts
LEFT OUTER JOIN taggings
ON posts
.id
= taggings
.taggable_id
AND taggings
.taggable_type
= 'Post' LEFT OUTER JOIN tags
ON tags
.type
= 'PostCategory' WHERE (tags
.name
LIKE '%eat%')
Thanks for the quick reply! :)
It looks like the id part of the join is being left off -- if you write the join manually:
puts Post.joins(:categories).to_sql
What do you get back? The joins will be inner joins in this case instead of outer joins, but otherwise should be similar.
Yes, I get inner joins like this:
SELECT posts
.* FROM posts
INNER JOIN taggings
ON posts
.id
= taggings
.taggable_id
AND taggings
.taggable_type
= 'Post' INNER JOIN tags
ON tags
.type
= 'PostCategory'
So how to I get the result I want that to find Posts which category name may contain string like "eat"?
If you are getting that from a join on your own, this isn't a MetaSearch bug. Sounds like either a bug in the association code or a bug in your association itself. You can try it again in the app without metasearch loaded at all to verify, of course.
Sent from my iPhone
On Sep 4, 2011, at 10:40 AM, regdog reply@reply.github.com wrote:
Yes, I get inner joins like this:
SELECT
posts
.* FROMposts
INNER JOINtaggings
ONposts
.id
=taggings
.taggable_id
ANDtaggings
.taggable_type
= 'Post' INNER JOINtags
ONtags
.type
= 'PostCategory'So how to I get the result I want that to find Posts which category name may contain string like "eat"?
Reply to this email directly or view it on GitHub: https://github.com/ernie/meta_search/issues/59#issuecomment-1991516
Ernie, thanks!
I verified the associations myself. It works fine without any errors. I just cannot figure out where is the problem.
I have a complex model design.
Say, the Tag model here uses single-table inheritance for different type of categories. The Tagging model use polymorphic assocation for taggings of different category types as well.
Given the following models:
class Tag < ActiveRecord::Base end
class PostCategory < Tag has_many :taggings, :foreign_key => "tag_id" has_many :posts, :through => :taggings end
class Tagging < ActiveRecord::Base belongs_to :taggable, :polymorphic => true
belongs_to :post, :foreign_key => "taggable_id" belongs_to :post_category, :foreign_key => "tag_id" end
class Post < ActiveRecord::Base has_many :taggings, :as => :taggable has_many :categories, :through => :taggings, :source => :post_category end
I want to find Posts which category name may contain string like "eat". e.g. @search = Post.search({:categories_name_contains=>"eat"})
It will return all the posts or none of the posts in database table, depending on whether the search string is contained in any of post category's name.
Is anything wrong with the way I named the search option or is this a Meta-Search limitation ?
Thanks Meta Search! It's a great gem!
Cameo