Thinking Sphinx Rails Multiple Association - ruby-on-rails

I have the following models
class Product < ActiveRecord::Base
belongs_to :sub_category
end
class SubCategory < ActiveRecord::Base
belongs_to :category
has_many :products
end
class Category < ActiveRecord::Base
has_many :sub_categories , -> { where("activate = 1") }
end
I need to index my products table.I need to search using category name(which is in category table) and subcategory name(in subcategories table)
ThinkingSphinx::Index.define :product , :with => :active_record do
indexes description
indexes name
indexes merchant_name
indexes sub_category(:sub_category) , :as => :sub_category_name
indexes category(:name) , :as => :cat_name
has sub_category_id
end
The category(:name) is failing.The subcategory is working fine.
Could somebody please help.I tried sub_category.category(:name) but thats also failing
Error Message
ERROR: index 'link_core': sql_range_query: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'AS cat_name, products.id AS
sphinx_internal_id, 'Product' AS `sphinx_internal_' at line 1
(DSN=mysql://root:***#localhost:3306/xxxx_dev_phase4)

name should be passed as a chained method, not as an argument
indexes sub_category.category.name , :as => "category_name"
Thanks to the owner Pat for helping me out
concerned github thread

Related

Categories specified in the model are not uploading to the database

I want to know what I'm doing wrong as my Categories "name" that I specify on rails console are not uploading directly to my postgresql database.
My first Model (category.rb)file
class Category < ApplicationRecord
attr_accessible :name
has_many :content
end
My Second Model (content.rb)file
class Content < ApplicationRecord
attr_accessible :title, :body, :category_id, :author_id
belongs_to :category
end
I've added two categories inside this model
"Football" and Cricket using the code below:
category = Category.create(:name => "Football")
category = Category.create(:name => "Cricket")
The above code creates category id and tables in the postgresql not the name I specified.
Please help.. Please reply if you need anything else
class Category < ApplicationRecord
attr_accessible :name
# has_many :content -- This is wrong
has_many :contents # This is how it should be
end
Also has hashrocket suggested try running migration.
What is the result of running the following in console?
c = Category.new
c.name = "football"
c.save
If this gives you an error then going through the error message will show what is wrong.

Multiple belongs_to to the same table

I have two tables:
currencies and rates
currencies: id:int, code:string, name: string
rates: id:int, top_currency_id:int, bottom_currency_id:int, rate:float
And I have two active records for them:
class Rate < ActiveRecord::Base
attr_accessible :bottom_currency, :rate, :top_currency, :top_currency_id
belongs_to :top_currency, :class_name => 'Currency', :foreign_key => 'top_currency_id'
belongs_to :bottom_currency, :class_name => 'Currency', :foreign_key => 'bottom_currency_id'
end
class Currency < ActiveRecord::Base
attr_accessible :code, :name
has_many :rates
end
So the problem is:
When I'm tring to execute following code:
top_currency = Currency.find_by_id(1)
#test = Rate.where(:top_currency=>top_currency)
I getting following error:
Mysql2::Error: Unknown column 'rates.top_currency' in
'where clause': SELECT `rates`.* FROM `rates` WHERE `rates`.`top_currency` = 1
Why Rails's magic doesn't work?
Many thanks.
In your two belongs_to methods, change the foreign_key option to primary_key, leaving everything else as is.
belongs_to :top_currency, :class_name => 'Currency', :primary_key => 'top_currency_id'
# ...
By default, an associated object's primary key is id. However, your currency model has three primary keys, the expected id plus two extra keys: top_currency_id and bottom_currency_id. Active Record needs to know which key to look for. Tell it with the primary_key option.
The foreign_key option is needed when a foreign key is different than the association's name (belongs_to :name) plus "_id". Since your foreign key matches the association name plus "_id," you do not need to use the foreign_key option.
From what I see, your code should work in theory. But I do think you are being a bit redundant.
It should be enough to just do this:
class Rate < ActiveRecord::Base
belongs_to :top_currency, class_name: 'Currency'
belongs_to :bottom_currency, class_name: 'Currency'
end
Rails will infer that the foreign key for top_currency is top_currency_id, and bottom_currency_id for bottom_currency.
I don't think you can query on the relationship like that. To use your example:
top_currency = Currency.find_by_id(1)
#test = Rate.where(:top_currency=>top_currency)
You'd have to change it to this:
top_currency = Currency.find_by_id(1)
#test = Rate.where(:top_currency_id => top_currency.id)
But it might just be easier to do this:
top_currency = Currency.find_by_id(1)
#test = top_currency.rates

Thinking-Sphinx Grouping Error

I have have the meeting model which belongs to the project:
class Project < ActiveRecord::Base
has_many :meetings
end
class Meeting < ActiveRecord::Base
belongs_to :project
define_index do
join project
indexes agenda
indexes project.name. :as => :project_name
end
end
I attempt to search with grouping:
Meeting.search("stuff", :group_by => 'project_id', :group_function => :attr)
I get the following error:
group-by attribute 'project_id' not found
Any suggestions?
Many Thanks.
This is just a wild guess based on the examples in the ThinkingSphinx docs (http://freelancing-god.github.com/ts/en/searching.html#grouping), but perhaps you need to include the attribute to be grouped by in the indexing.
Try adding has project_id to your define_index.

No Such Column - When Column Exists

Not sure how this is happening but it's saying the column doesn't exist:
SQLite3::SQLException: no such column: element.kind: SELECT COUNT(*) FROM "answers" INNER JOIN "elements" ON "elements"."id" = "answers"."element_id" WHERE "answers"."form_id" = 55 AND "element"."kind" = 6
# element.rb
class Element < ActiveRecord::Base
has_many :answers
end
# answer.rb
class Answer < ActiveRecord::Base
belongs_to :element
belongs_to :form
end
class Form < ActiveRecord::Base
has_many :answers
end
But when I run:
#form.answers.joins(:element).where(:element => {:kind => 6})
I get the sql error above. Not sure what's going on. Any thoughts on what I'm missing?
Thanks!
FYI I'm running rails 3.2.3 with ruby 1.9.3.
The table is elements rather than element as generated by the query ("element"."kind" = 6).
#form.answers.joins(:elements).where(:elements => {:kind => 6})
I would have expected the rest of the query to be generated using the nonexistent element table as well, since you used .joins(:element) instead of .joins(:elements) but perhaps Rails is pluralizing inside .joins() for a belongs_to association.
#form.answers.joins(:element).where(:elements => {:kind => 6})

Rails 3 - Eager loading on a legacy database

I have inherited a web app with the following tables: Categories, SubCategories and Pages. Pages has category_id and sub_category_id columns.
I need to write an efficient query to eager load pages by category and sub_category for iterate in my view (crude example follows):
- Category One (categories.each do |category|...)
-- Page One (category.pages.each do |page|...)
-- SubCategory One (category.sub_categories.each do |sub_category|...
---- Page Two (sub_category.pages.each do |page|...)
Category.rb:
class Category < ActiveRecord::Base
has_many :pages
has_many :sub_categories
end
SubCategory.rb:
class SubCategory < ActiveRecord::Base
belongs_to :category
has_many :pages
end
Page.rb:
class Page < ActiveRecord::Base
belongs_to :category
belongs_to :sub_category
scope :active_pages, :conditions => {:is_active => true}
end
I've experimented with queries like the following with little success where the sub_categories are concerned:
Category.includes(:sub_categories, :pages).where('pages.is_active = 1')
Categories works great, but I'm not sure how to eager load the sub_categories. Thanks in advance, any help is greatly appreciate.
according to this article which I was just looking at something like this might do what you want:
Category.find( :all, :include => [ :pages, { :sub_categories => :pages } ] )

Resources