SQL Query in two different Rails models that have no association - sql

I have a landlords table, and landlord_addresses table, and a landlord_companies table. From the landlords index view and using the landlords_controller I need to be able to search the landlord_companies, BUT landlords and landlord_companies have no ActiveRecord association with each other. Obviously I can't use what I've written below, but I am not sure how to search the landlord_companies ...any help would be great!
#landlord = #landlords.includes(:landlord_company)
.references(:landlord_companies)
.where("landlord_companies.llc_name LIKE ?", "%#{params[:landlord_llc_search]}%")
Schema:
create_table "landlords", force: :cascade do |t|
t.string "name"
t.string "contact_name"
t.string "contact_number"
t.integer "listing_agent_id"
t.boolean "own_application"
t.boolean "own_credit"
t.boolean "accepts_roommate_matchups"
t.boolean "management_company"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "landlord_companies", force: :cascade do |t|
t.string "llc_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "landlord_address_id"
end

I was able to figure this out and actually use the SQL query I had already written. I thought there was no association, but technically there was using a through.
Landlord.rb
has_many :landlord_companies, through: :landlord_addresses

Related

How to Sort By Association Attribute Value in Rails

I'm sure I'm missing something basic, but I have a model User which belongs_to a Company model which has a name attribute.
How can I sort all users by their Company name?
#users = User.all #sort this collection in order of the associated company's name
class User < ActiveRecord::Base
belongs_to :company
end
class Company < ActiveRecord::Base
end
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "company_id"
end
create_table "companies", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
User.includes(:company).order('company.name') . I believe ASC is default, but you can add it if necessary. You may also have to specify the Company table name in the model, or change the table name to 'companies', as that is the default pluralization convention over configuration will assume I believe.

How to import data into rails models from a table in a database

I have the following schema in my rails app:
ActiveRecord::Schema.define(version: 20161020060112) do
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 "languages", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "quotes", force: :cascade do |t|
t.string "title"
t.date "date"
t.string "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "author_id"
t.integer "language_id"
t.integer "source_id"
t.index ["author_id"], name: "index_quotes_on_author_id"
t.index ["language_id"], name: "index_quotes_on_language_id"
t.index ["source_id"], name: "index_quotes_on_source_id"
end
create_table "source_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sources", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "source_type_id"
t.index ["source_type_id"], name: "index_sources_on_source_type_id"
end
end
And this is the schema of the table that I want to import data from:
CREATE TABLE quotation_data(
id integer primary key,
author_name text,
source_type text,
source text,
language text,
date text,
title text,
body text
);
I did find some posts on Stackoverflow but none that tell about importing data from a single table into fields distributed over many models.
Thanks!
I have imported data from database I create the list that list everything that I have inserted in the database.
like:
Under controller#show
def show
#quotes = Quote.all
end
views#show
<ul>
<% #quotes.each do |quote| %>
<li>
<%= link_to quotes.title, edit_quotes_path(quote) %>
</li>
<% end %>
</ul>
This will help you to view what is on the database and edit it also.

Most efficient way to get a Users Tags

I have a somewhat complicated database structure in a Ruby on Rails project. A User can have many EmailAccounts, an EmailAccount can have many Subscriptions (many-to-many via a join table), and a Subscription can have many Tags (also many-to-many)
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.date "dob"
t.string "gender"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "email_accounts", force: :cascade do |t|
t.integer "user_id"
t.string "email"
t.string "provider"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "account_subscriptions", force: :cascade do |t|
t.integer "email_account_id", null: false
t.integer "subscription_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "subscriptions", force: :cascade do |t|
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "domain"
end
create_table "sub_tags", force: :cascade do |t|
t.integer "subscription_id", null: false
t.integer "tag_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
I want to see which Tags apply to a User through this relationship. Whats the most efficient way to do so (eager load all the relationships and loop through them, do a multi-level join in Postgres, etc)?
Assuming you have the right models
user = User.find(user_id)
tags = Tag.where(:id => SubTag.where(:subscription_id => AccountSubscription.where(:email_account_id => user.email_accounts.map{|e| e.id}).map{|a_s| a_s.subscription_id}).map{|s_t| s_t.tag_id})
tag_name = tags.map{|t| t.name}
this should work fine given you have the right indexes.

How can i use data from antoher tables in a active record where in rails

im trying to make a search form in a table that contains two ids as values, one of them is the id of an user and the other is the id of a project.
Here is my database schema:
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.text "bio"
t.float "lastLatitude"
t.float "lastLongitude"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "swipes"
t.string "profile_image"
end
create_table "projects", force: :cascade do |t|
t.string "name"
t.text "bio"
t.integer "user_id"
t.string "salary"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "likes"
t.string "image"
t.float "latitude"
t.float "longitude"
end
create_table "matches", force: :cascade do |t|
t.integer "user_id"
t.integer "project_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
What i want to do is to create a search form in the matches index view to filter the matches by the user name or the project name, but i don't know how to do it, this is what i have right now:
def self.search(search)
if search
where('User(user_id).name LIKE :search OR Project.name LIKE :search', search: "%#{search}%")
else
where(nil)
end
end
Is it possible to do that?
Any help is welcome
I suggest a scope approach
class Match < ActiveRecord::Base
belongs_to :project
belongs_to :user
scope name, ->(n) {
joins(:user, :project).where("users.name like ? or projects.name like ? ", "%#{n}%", "%#{n}%")
}
end
class User < ActiveRecord::Base
has_many :projects
scope :name, ->(n) {
where("users.name like ?", "%n%")
}
end
class Project < ActiveRecord::Base
scope :name, ->(n) {
where("projects.name like ?", "%n%")
}
end

Rails find with three tables and a SUM operation

I'm a little stumped as to get the order of records I want with a find operation.
Let's say you had three models:
1. Websites
2. Links
3. Votes
A website has many links and a link has many votes. Each vote has a certain amount of points that a user can attribute to that vote. I'm trying to get a website index page where websites are listed in order of the sum of the points they've received for all the links for that website.
Here's a simplified version of the schema
create_table "votes", :force => true do |t|
t.integer "link_id"
t.integer "points"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "links", :force => true do |t|
t.string "name"
t.string "link"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "votes_count", :default => 0
t.integer "website_id"
end
create_table "websites", :force => true do |t|
t.string "domain"
t.boolean "verified", :default => false
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
I'm trying to think about the right active record query to use here. Any help would be appreciated.
To think that I spent a day pulling my hair out when I came up with a solution just an hour after I posted this question.
In the website model I put that the website has_many :points, through => :links
Then the query:
array = Website.find(:all)
array.sort_by {|w| w.donations.sum('amount')}.reverse
This seems to work.