Passing database querying tests using spec? - ruby-on-rails

I am learning rspec and databases and have one movie database with the following schema. I'd like to know how to get this test to pass since my efforts seem to be futile. If you need any additional info please let me know in case I'm leaving something vital out that would be helpful:
Schema
create_table "movies", force: true do |t|
t.string "title", null: false
t.integer "year", null: false
t.text "synopsis"
t.integer "rating"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "genre_id", null: false
t.in
end
create_table "genres", force: true do |t|
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
Model setup:
class Movie < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :year
validates_presence_of :genre
has_many :cast_members
has_many :actors, through: :cast_members
belongs_to :genre
belongs_to :studio
Rspec test:
it "partially matches movie titles" do
results = Movie.search('Manhat')
expect(results.count).to eq(2)
expect(results.include?(manhattan)).to be_true
expect(results.include?(mystery)).to be_true
end
Rspec test argument input:
.create(:movie, title: "Manhattan Murder Mystery"
Code I've tried several variations of so far:
class Movie < ActiveRecord::Base
etc, above..
def self.search(query)
select('title').where('title ilike ? OR synopsis ilike ?','%query%', '%query%')
end

You are not passing the parameter to the query. You are always searching for results containing "query". What you should do is:
select('title').where('title ilike ? OR synopsis ilike ?',"%#{query}%", "%#{query}%")
This will substitute the "#{query}" with the parameter passed to the search method.

Related

Rails associations with search

I have issue with associations with search
class Manufacturer < ApplicationRecord
has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy
def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
end
and
class ManufacturerTranslation < ApplicationRecord
belongs_to :manufacturer
end
so when i have tried to do search and call translations on it
Manufacturer.search('fra').last.translations
it gives me only translations with name which include fra, not all of translations
so i have 6 translations in total for this manufacturer
but after search a get only 2
database schema
create_table "manufacturer_translations" do |t|
t.integer "manufacturer_id"
t.string "locale"
t.string "name"
t.string "image_source_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manufacturer_id"], name: "index_manufacturer_translations_on_manufacturer_id"
t.index ["name"], name: "index_manufacturer_translations_on_name"
end
create_table "manufacturers", do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "deleted", default: false
end
As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:
def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
conditions = ''
q.each do |qu|
conditions = "#{conditions} OR " if conditions.present?
conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
end
joins(:translations).where(conditions)
end
Assuming you follow Rails conventions I'd suggest you to replace this
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
with
joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")
May be needed to play with quotations

Has_many through query

I have 2 models (Books and Authors) and a third table joining them (has_many through association).
I am trying to implement search in my app and run a query on both tables. My query looks like this and I cannot figure out the problem:
Book.includes(:authors, :author_books).where("books.title LIKE ? OR authors.name = LIKE ?", "%#{book}%", "%#{book}%")
This is the error that I get running it:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "authors"
SELECT "books".* FROM "books" WHERE (books.title LIKE '%Harry%' OR authors.name = LIKE '%Harry%')
Here is my schema of the three tables:
create_table "author_books", force: :cascade do |t|
t.bigint "author_id"
t.bigint "book_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["author_id"], name: "index_author_books_on_author_id"
t.index ["book_id"], name: "index_author_books_on_book_id"
end
create_table "authors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "books", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "rating"
t.string "critics"
t.float "price"
end
author_book.rb
class AuthorBook < ApplicationRecord
validates_presence_of :author, :book
belongs_to :author
belongs_to :book
end
author.rb
class Author < ApplicationRecord
validates :name, uniqueness: true
has_many :author_book
has_many :books, through: :author_book
end
book.rb
class Book < ApplicationRecord
validates :title, uniqueness: true, :case_sensitive => false
has_many :author_book
has_many :authors, through: :author_book
has_many :categories, through: :category_book
def self.search_book(book)
if book
Book.joins(:authors, :author_books).includes(:authors, :author_books).where("books.title LIKE ? OR authors.name = LIKE ?", "%#{book}%", "%#{book}%")
end
end
end
I call this search_book method in my book controller like so:
def search
#books = Book.search_book(params[:book])
end
Some help, please?
Thanks!
From the docs
If you want to add conditions to your included models you’ll have to
explicitly reference them.
That said, you need to add references(:authors) to your query like below to resolve the error
Book.includes(:authors, :author_books).where("books.title LIKE ? OR authors.name = LIKE ?", "%#{book}%", "%#{book}%").references(:authors)
Update:
Can't join 'Book' to association named 'author_books'; perhaps you
misspelled it?
You should replace has_many :author_book with has_many :author_books and through: :author_book with through: :author_books
You forgot to join authors and author_books to your relation. includes loads both :author and :author_books but in separate queries.
Try this:
Book.joins(:authors, :author_books).includes(:authors, :author_books).where("books.title LIKE ? OR authors.name = LIKE ?", "%#{book}%", "%#{book}%")

All Rails active record foreign keys are nil after import via activerecord-import gem

Ruby 2.3.0, Rails 4.2.4, PostgreSQL 9.5
UPDATE: added activerecord-import code below.
Does anyone know how to make these associations hold, so that a model's table attributes can be referenced in another view? Similar to another Q&A (Rails has_many through aliasing with source and source_type for multiple types), where I have investors, companies, and transactions.
I've tried associations like the below (has_many ... through ...), but I'm failing to get ActiveRecord to recognize the connection among the 3 models & tables. Seeding the db:
The way data gets into these tables is via a csv file having 3 columns. I use roo-xls to extract each into an array of arrays.
My activerecord-import gem-based code (each *_val is an array of 1000s of arrays):
icol = [:name]
ccol = [:name]
tcol = [:investor_name, :company_name, :percent_owned]
investor_val = [["i1"],["i2"]] # just showing 2 arrays for brevity
company_val = [["c1"],["c2"]] # ""
transaction_val = [["i1","c1","pct1"],["i2","c2","pct2"]] # ""
Investor.import icol, investor_val, :validate => false
Company.import ccol, company_val, :validate => false
Transaction.import tcol, transaction_val, :validate => false
Import works, but when I check the transactions table, both company_id and investor_id are nil after executing the activerecord-import .import. I of course would like them to contain the foreign keys for the company and investor model records.
My models are below.
Class Company < ActiveRecord::Base
has_many :investors,
:through => :transactions
has_many :transactions
end
Class Investor < ActiveRecord::Base
has_many :companies,
:through => :transactions
has_many :transactions
end
Class Transaction < ActiveRecord::Base
belongs_to :company
belongs_to :investor
end
Transactions migration (others left out for brevity)
class CreatePositions < ActiveRecord::Migration
def change
create_table :positions do |t|
t.string :investor_name
t.string :company_name
t.string :percent_owned
t.belongs_to :company, index: true
t.belongs_to :manager, index: true
t.timestamps null: false
end
end
end
My schema, where I've added references to the belongs_to (transactions) table.
ActiveRecord::Schema.define(version: 20160128224843) do
create_table "companies", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "investors", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "transactions", force: :cascade do |t|
t.string "investor_name"
t.string "company_name"
t.float "percent_owned"
t.integer "investor_id"
t.integer "company_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "transactions", ["investor_id"], name: "index_transactions_on_investor_id", using: :btree
add_index "transactions", ["company_id"], name: "index_transactions_on_company_id", using: :btree

Which relationship is good to use STI or Polymorphism?

I have read about them but still not clear to me which one I suppose to use and how.
I have User model, Message model and Place model
Message model:
class Message < ActiveRecord::Base
belongs_to :user
end
Messages Table:
create_table "messages", force: true do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
User model:
class User < ActiveRecord::Base
has_many :messages
end
Users Table:
create_table "users", force: true do |t|
t.string "email"
t.string "password_digest"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
Now, what I want to do is:
"USER" says "MESSAGES" from "PLACES"
eg. "AHMED" says "HELLO" from "EARTH"
For me both Models (Message and Place) have same data (data type) and same behaviours. So places table should be:
create_table "places", force: true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Now may be I'm confused or making big deal than it should be.
What kind of relation should Message and Place have? should it be STI or Polymorphism?
How should I decide?
I'd appreciate the thinking process of how and why I decide specific association.
This example, despite Messages and Places having the same data, doesn't seems a STI/Polymorphism scenario and they should have two different tables.
This could work as a solution:
create_table "users" do |t|
t.string "username"
end
create_table "messages" do |t|
t.string "text"
t.integer "user_id"
t.integer "place_id"
end
create_table "places" do |t|
t.string "name"
end
class User < ActiveRecord::Base
has_many :messages
has_many :places, through: :messages
end
class Place < ActiveRecord::Base
end
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :place
def to_s
"#{user.username} says #{title} from #{place.name}"
end
end
ahmed = User.new(username: "AHMED")
earth = Place.new(name: "EARTH")
message = Message.new(text: "HELLO", user: ahmed, place: earth)
puts message
# => "AHMED says HELLO from EARTH"

TypeError Conversion on has_many relationship

I have a couple of objects in a Rails app ("Ticket", and "Comment")
class Ticket < ActiveRecord::Base
has_many :attributes
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :ticket
belongs_to :user
end
with the following schema:
create_table "comments", :force => true do |t|
t.integer "ticket_id"
t.integer "user_id"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tickets", :force => true do |t|
t.integer "site_id"
t.integer "status"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
However, for some reason - whenever I do a #lead.comments I get a crash:
can't convert String into Integer
Any ideas? This is driving me nuts!
I think the line that's causing you pronlems is:
has_many :attributes
"attributes" is a special word in an Active Record. It refers to the values of the columns in the db.
If you try and override this with an association, then you will have problems.
My suggestion is that you should not have a model called an "attribute" - call it something else, eg "properties", and the problems will go away.

Resources