Thinking sphinx search doesnt return any result - ruby-on-rails

i'm trying to implement simple search function with thinking sphinx, basing on this. i managed to set sphinx up, but when i try it in a console, 0 results are found every time. i'm wondering what is wrong here...
indices/news_post_index.rb:
ThinkingSphinx::Index.define :news_post, :with => :active_record do
indexes title
indexes content
end
and the rails console output:
2.0.0p0 :013 > NewsPost.create!(title: "awesome post", content: "this post is awesome and you know it", published_at: "2001-01-01")
(0.3ms) BEGIN
NewsPost Exists (60.7ms) SELECT 1 AS one FROM `news_posts` WHERE `news_posts`.`slug` = 'awesome-post' LIMIT 1
SQL (48.3ms) INSERT INTO `news_posts` (`content`, `created_at`, `published_at`, `slug`, `title`, `updated_at`) VALUES ('this post is awesome and you know it', '2013-09-10 08:18:35', '2001-01-01', 'awesome-post', 'awesome post', '2013-09-10 08:18:35')
(40.1ms) COMMIT
=> #<NewsPost id: 2, published_at: "2001-01-01", content: "this post is awesome and you know it", title: "awesome post", created_at: "2013-09-10 08:18:35", updated_at: "2013-09-10 08:18:35", slug: "awesome-post">
2.0.0p0 :014 > NewsPost.search "awesome post"
Sphinx Query (0.9ms) SELECT * FROM `news_post_core` WHERE MATCH('awesome post #sphinx_internal_class_name (NewsPost)') AND `sphinx_deleted` = 0 LIMIT 0, 20
Sphinx Found 0 results
=> []
From what i understand title is indexed, and so I should be able to search for it. but no matter what my key is, results are always empty array, no results at all...

Run rake ts:index after every update, then search.

Related

Could not find_by an object from database in Rails job

I have created a Ruby on Rails job (extending from ActiveJob::Base) that parses a CSV file and inserts its rows as records (Students) in a database. That is working as expected.
However, when I try to fetch another object from the DB (the Career for each Student, which is part of each CSV row as a pair (career_code, career_name)), I'm getting this error:
undefined method 'find_by' for Career:Class
(I have also tried using Career.where instead).
I find this quite strange, since I'm already saving my Student, which is also an ActiveRecord::Base child class.
Here's the relevant part of my job:
ActiveRecord::Base.transaction do
student_hash.keys.each do |k|
some_student = student_hash[k]
student = Student.new
student.csv_id = some_student.id
student.first_name = some_student.first_name
student.last_name = some_student.last_name
student.file_number = some_student.file_number
# student.career = Career.where(code: some_student.career_code)
student.career = Career.find_by code: some_student.career_code
puts student.save! # Why this works, and the above line doesn't?
end
end
And also, the desired output, as I can see it in the Rails console:
Loading development environment (Rails 4.2.4.rc1)
2.1.3 :001 > Career.where(code: 11)
Career Load (0.5ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? [["code", 11]]
=> #<ActiveRecord::Relation [#<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>]>
2.1.3 :002 > Career.where code: 11
Career Load (0.2ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? [["code", 11]]
=> #<ActiveRecord::Relation [#<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>]>
2.1.3 :003 > Career.find_by code: 11
Career Load (0.4ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? LIMIT 1 [["code", 11]]
=> #<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>
2.1.3 :004 >
Probably this is a really stupid question, but I'm quite a beginner using Rails. Do I need to import some sort of "Context" (this seems unlikely, since Student seems to be properly resolved)? I'm using Rails 4.2.4.rc1 by the way.
Thanks in advance
Any chance at that point in the code Career isn't an AR, but some other class? That would at least explain why the methods don't exist.

Ruby on Rails: weird behavior in new, un-persisted model

I'm at a loss. We have two classes, Sample and Workflow, that have a many-to-many relationship, using has_many, :through.
At Rails Console, I get the following:
[3] pry(main)> s = Sample.find(1)
Sample Load (0.2ms) SELECT `samples`.* FROM `samples` WHERE `samples`.`id` = 1 LIMIT 1
=> #<Sample id: 1, name: "Some sample", experiment_type: "Some type", category_id: nil,
created_at: "2014-01-14 21:24:14", updated_at: "2014-01-14 21:24:14">
[4] pry(main)> wf = Workflow.new
=> #<Workflow id: nil, name: nil, description: nil>
[5] pry(main)> wf.samples.include?(s)
Sample Exists (0.2ms) SELECT 1 AS one FROM `samples` INNER JOIN `sample_workflows` ON
`samples`.`id` = `sample_workflows`.`sample_id` WHERE `sample_workflows`.`workflow_id` IS NULL
AND `samples`.`id` = 1 LIMIT 1
=> true
[6] pry(main)> wf.samples
=> []
[7] pry(main)> wf.samples.include?(s)
=> false
wf is a brand new workflow, should not have any samples attached to it. wf.samples.include? indicates that sample 1 is already linked to the workflow. But if I try to look at the samples, the array comes back blank, and further include? queries return false (as they should). The same thing happens for sample 2, but not for any others I've tried. Is there some way I could have made the workflow link to two samples by default by mistake?

How to show values of a instance with line break in Rails console

I start to using pry in a rails console.
When I get a instance of a Rails model, the values are shown without line breaks like this:
pry(#<Class:0x1022f60e0>):1> first
=> #<Article id: 1, name: "What is Music", content: "Music is an art form in which the medium is sound o...", created_at: "2011-08-24 20:35:29", updated_at: "2011-08-24 20:37:22", published_at: "2011-05-13 23:00:00">
from http://railscasts.com/episodes/280-pry-with-rails?view=asciicast
Is there way to show the values with line breaks like this?
Article
id: 1
name: "What is Music"
content: "Music is an art form in which the medium is sound o..."
created_at: "2011-08-24 20:35:29"
updated_at: "2011-08-24 20:37:22"
published_at: "2011-05-13 23:00:00"
You could call .to_yaml on the model instance! It returns a string that's formatted almost exactly like you're requesting it to be.
Here are some examples of to_yaml output:
http://yaml4r.sourceforge.net/doc/page/examples.htm
I would recommend that you install awesome_print.
Add it to your Gemfile:
group :development do
gem 'awesome_print'
end
And install it with bundle install.
Now use ap to print it in the console:
pry(#<Class:0x1022f60e0>):1> ap first
#<Article:0x1022f60e0> {
:id => 1,
:name => "What is Music"
:content => "Music is an art form in which the medium is sound o..."
:created_at => "2011-08-24 20:35:29"
:updated_at => "2011-08-24 20:37:22"
:published_at => "2011-05-13 23:00:00"
}
I think, the below trick will work for you.
arup#linux-wzza:~/Rails/model_prac> rails c
Loading development environment (Rails 4.1.4)
2.1.2 :001 > Comment.first
Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1
=> #<Comment id: 1, value_old: "I am a good Boy.", value_new: "I am a bad Boy.", created_at: "2014-08-02 17:36:14", updated_at: "2014-08-02 18:21:42">
2.1.2 :002 > y Comment.first
Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1
--- !ruby/object:Comment
attributes:
id: 1
value_old: I am a good Boy.
value_new: I am a bad Boy.
created_at: 2014-08-02 17:36:14.249466000 Z
updated_at: 2014-08-02 18:21:42.511522000 Z
=> nil
2.1.2 :003 >

Rails paper clip search file name

I have a requirement for users to be able to search file names uploaded by paperclip. Looking at an uploaded document looks like this...
2.0.0-p353 :002 > d=Document.last
Document Load (0.4ms) SELECT "documents".* FROM "documents" ORDER BY "documents"."id" DESC LIMIT 1
=> #<Document id: 5, name: "hampton_george_27_70", transcription: "Asheville Nov 8th 1853 received of James A. Patton...", created_at: "2014-08-22 16:10:19", updated_at: "2014-08-22 16:37:51", document_file_name: "hampton_george_27_70.jpeg", document_content_type: "image/jpeg", document_file_size: 519652, document_updated_at: "2014-08-22 16:10:19">
2.0.0-p353 :003 > d.document_file_name
=> "hampton_george_27_70.jpeg"
But these are no traditional rails type models, so how would I go about searching them?
Document appears to be a model, so you can query it. For example, to search for documents with "george" in the file name:
search_term = "george"
documents = Document.where(['document_file_name LIKE ?', "%#{search_term}%"])

Rails boolean values and callbacks to default to false

UPDATE:
If the after_validation callback is used, it works as desired (e.g. the false value is persistent). Still would like to know why that is, but I guess this is solved for my purposes :)
For a boolean field, I would like a callback in the model to set the default value to false instead of nil.
Currently when I create a new record, it initially shows the value as false, but then shows it as nil.
Wondering what's going on here and if the desired behavior is possible w/ a callback.
This is in the model:
after_save :default_is_forsale
def default_is_forsale
self.not_for_sale = false if self.not_for_sale.nil?
end
Here is the rails console output (irrelevant bits omitted):
1.9.3p125 :001 > Item.create(name: "thing 4")
(0.1ms) begin transaction
SQL (6.4ms) INSERT INTO items [...]
(190.8ms) commit transaction
=> #<Item id: 20, name: "thing 4", not_for_sale: false>
Cool, created the new record with a default value of false. But when I check again:
1.9.3p125 :002 > Item.last
Item Load (0.3ms) SELECT [...]
=> #<Item id: 20, name: "thing 4", not_for_sale: nil>
Weird, now the value is nil.
But if I create a new record and explicitly set the value to false, it acts as I'd expect:
1.9.3p125 :003 > Item.create(name: "more thing", not_for_sale: false)
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO items [...]
(225.2ms) commit transaction
=> #<Item id: 21, name: "more thing", not_for_sale: false>
When retrieved, the record still shows its boolean value of false
1.9.3p125 :004 > Item.last
Item Load (0.3ms) SELECT [...]
=> #<Item id: 21, name: "more thing", not_for_sale: false>
BTW, I read elsewhere that the desired result is achievable via db migrations, but I am new to rails and would like to accomplish it through the model.
Thanks
Change your migration to set this boolean to false, as default. if there was code i'd show you.
I just read you were 'new to rails' but that doesn't matter. You don't need to do it in the model, unless you want that record to be true.
You want this in a before_save callback. The after_save callback is called, unsurprisingly, after the record has already been saved.

Resources