Get id, name from master table - Ruby - ruby-on-rails

The association is has follows
Company has_many company_commodities
CompanyCommodity belongs to Company
CompanyCommodity belongs to Commodity
Consider that company1 has an entry in the company_commodities table.
Now in the decorator file, i need to get the commodity name and id of that record.
I have implemented as follows.
company1 = Company.find(1)
arr = co.company_commodities.map(&:commodity).pluck(:name, :id)
arr.map { |a| { name: a[0], id: a[1] } }
This produces the output as
[{:name=>"Pharmaceuticals", :id=>25},
{:name=>"Medical Devices", :id=>26}]
Is there a cleaner way to do this? 

class Company < ApplicationRecord
has_many :company_commodities
has_many :commodities, through: :company_commodities
end
class CompanyCommodity < ApplicationRecord
belongs_to :commodity
belongs_to :company
end
company = Company.find(1)
# this returns the object you can access by arr.first.id, arr.first.name
arr = company.commodities.select(:name, :id)
# if you want to access as hash, usage: arr.first['id'], arr.first['name']
arr = company.commodities.select(:name, :id).as_json
You can get the commodities association by using through, then you can use select to filter the attributes.

Change the associations as below
class Company
has_many :company_commodities
has_many :commodities, through: :company_commodities
end
class CompanyCommodity
belongs_to :company
belongs_to :commodity
end
And query the records as
company1 = Company.find(1)
arr = co.commodities.pluck(:name, :id)
arr.reduce([]) { |array, el| array << [[:name, :id], el].to_h}

Related

Destroy deep associated object on parent save, using assign_attributes

I want to delete an deep associated record inside assign_attributes.
Screen is the only object I need to save, but the deep associated NoteMember object should get deleted on save of Screen object, if params[:delete] is true for that particular NoteMember object.
Following is the table structure:
MODELS
class Screen < ActiveRecord::Base
has_many :alerts
accepts_nested_attributes_for :alerts
end
class Alert < ActiveRecord::Base
has_many :notes
accepts_nested_attributes_for :notes
end
class Note < ActiveRecord::Base
has_many :note_members
accepts_nested_attributes_for :note_members
end
class NoteMember < ActiveRecord::Base
end
CONTROLLER
s = Screen.where(id: <some_id>).first
alert_attrs = []
params[:alerts].each do |a|
notes_attrs = []
params[:notes].each do |n|
note_member_attrs = []
params[:note_members].each do |nm|
# if nm[:delete] = true, I need to delete the note member on saving Screen object
note_member_attrs.push({
id: nm[:id],
visibility: nm[:visibility]
})
end
notes_attrs.push({
id: n[:id],
description: n[:description],
note_members_attributes: note_member_attrs
})
end
alert_attrs.push({
id: a[:id],
name: a[:name]
notes_attributes: notes_attrs
})
end
s.assign_attributes(
alerts_attributes: alert_attrs
)
s.save!
How can this be achieved?
You can use rails built-in destroy functionality:
accepts_nested_attributes_for :note_members, allow_destroy: true
and pass
note_members_attributes: [ { _destroy: true, id: 123 }]
for note_members that need to be deleted.

rails scope without associations

I have a model called "activos", I need to show only the records that are not associated with another model called "relactivo".
I've been trying this in the model: scope :ts, -> { includes(:relactivo).where(relactivo: { activo: nil}) }
this is my model "activos"
class Activo < ActiveRecord::Base
self.primary_key = "IdActivos"
scope :ts, -> { includes(:relactivo).where(relactivo: { activo: nil}) }
has_one :relactivo, class_name: "Relactivo", foreign_key: "Activo"
end
and my model "relactivo"
class Relactivo < ActiveRecord::Base
self.primary_key = "IdRow"
belongs_to :activo, class_name:"Activo", foreign_key: "Activo"
end
Try doing this for your scope:
class Activo < ActiveRecord::Base
self.primary_key = "IdActivos"
scope :ts, -> { joins('LEFT OUTER JOIN relactivos ON relactivos.IdActivos = activos.IdActivos WHERE relactivos.IdActivos IS null'))) }
has_one :relactivo, class_name: "Relactivo", foreign_key: "IdActivos"
end
See if this works, The custom primary/foreign key makes it a little strange but try this.

Scope with 2 different conditions

I would like to filter stories on my index based on 2 different conditions where one is for the Current Country and the other one is for All Countries. Is it possible to create a scope where it could fetch stories for both this condition ?
All Countries is boolean field where in my Story table. The logic is if the Story is created for all countries the field, all_countries = 1
Featured Item model, is where the stories could be featured on the index page if the writer would like to do so.
This is how my model looks like for now with the scopes
class Country < ActiveRecord::Base
has_many :stories
end
class Story < ActiveRecord::Base
belongs_to :countries
has_many :featured_items, dependent: :destroy
scope :by_country, lambda { |id| where(:country_id => id)}
scope :for_all_countries, where(:all_countries => true)
end
class FeaturedItem < ActiveRecord::Base
belongs_to :story
scope :by_country, -> (country) {joins(:story).where('`stories`.country_id = ?', Country.find(country) )}
scope :for_all_countries, -> { joins(:story).where('`stories`.all_countries = ?',true) }
end
p/s the scope for all countries on the featured Items also returns an error.
You can do this sort of thing:
scope :by_country, -> (country) { country == :all ? where(:all_countries => true) : where(:country_id => country) }
You may need to add a little more logic to handle bad params.
And for the join table, you can join and filter on the stories.
class FeaturedItem < ActiveRecord::Base
scope :by_country, -> (country) { (country == :all ? where( :stories => { :all_countries => true } ) : where( :stories => { :country_id => country } ) ).joins(:story) }
end
Your scope syntax is currently wrong, as is your pluralization of the belongs_to association.
You'll need to use the following (#swards answer is right, this is just an addition):
#app/models/story.rb
class Story < ActiveRecord::Base
belongs_to :country
scope :countries, ->(ids = :all) { ids == :all ? where(all_countries: true) : find(ids) }
end
This will allow you to call Story.countries to return all countries, and Story.countries(1,2,4,5) to return individual ones.
filter stories on my index based on 2 different conditions where one is for the Current Country and the other one is for All Countries.
Have you considered using the following in your Country model:
#stories = #country ? #country.stories : Country.stories
#app/models/country.rb
class Country < ActiveRecord::Base
has_many :stories
scope :stories, -> { joins(:stories).where(story: {all_countries: true}) }
end

Rails querying many-to-many

Quick question here. Given the following example many-to-many relationship, how would I query the Physician table for appointments they have today?
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
On the Physician model I have the following:
scope :for, -> (name) { find_by_name(name: name) }
# I need a hand here, the join I assumed would work didn't seem to filter properly.
# scope :appointments_today, -> { joins(:appointment).where("appointments.appointment_date = ?", Date.today) }
scope :appointments_today, -> { ??? }
I'd like to chain queries on the controller as such:
data = Physician.for("test").appointments_today
Do you want a list of Physician records that have an appointment scheduled for today, or do you want a list of Appointment records that are for a specific physician and are scheduled for today?
Physicians that have an appointment today:
Physician.
joins(:appointments).
where(
name: "test",
appointments: {
appointment_date: (Date.today.beginning_of_day..Date.today.end_of_day)
}
)
Appointments for a physician that are today:
Appointment.
joins(:physician).
where(
appointment_date: (Date.today.beginning_of_day..Date.today.end_of_day),
physicians: { name: "test" }
)
As scopes, you can do physicians that have an appointment today:
class Physician < ActiveRecord::Base
scope :named, -> (name) { where(name: name) }
scope :with_appointments_on, -> (date) { joins(:appointments).where(appointments: { appointment_date: (date.beginning_of_day..date.end_of_day) })}
end
Physician.named("test").with_appointments_on(Date.today)
Or appointments for a physician that are today:
class Appointment < ActiveRecord::Base
scope :on_date, -> (date) { where(appointment_date: (date.beginning_of_day..date.end_of_day)) }
end
Physician.find_by_name("test").appointments.on_date(Date.today)
Try to do this
scope :for, -> (name) { where(name: name) }
Next, you have to add plural into appointments
scope :appointments_today, -> { joins(:appointments).where("appointments.appointment_date = ?", Date.today) }
I hope this help you.
If you are trying to fetch a list of appointments, the logic should go in the Appointment model:
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
scope :for_today, -> { where('appointments.appointment_date >= ? AND appointments.appointment_date < ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day) }
scope :for_physician, -> (name) { joins(:physician).where(physicians: {name: name}) }
end
And the you can find the appointments by:
data = Appointment.for_today.for_physician("test")

Rails filtering resource one to many through a joint table

I have the following resources:
- restaurant
- category
- item
- check item
Relationship:
class Restaurant < ActiveRecord::Base
has_many :items
has_many :categories
has_many :check_items
class Category < ActiveRecord::Base
belongs_to :restaurant
has_many :items
class Item < ActiveRecord::Base
belongs_to :restaurant
belongs_to :category
class CheckItem < ActiveRecord::Base
belongs_to :item
I need to filter all the check_items of a restaurant where category.uuid = '123123'
so I have my #restaurant.check_items. How do I join these together to basically implement this sql query:
SELECT * from checkitem
INNER JOIN item ON(checkitem.item_id = item.id)
INNER JOIN category ON(category.id = item.category_id)
WHERE category.restaurant_id = 1 AND category.uuid = '123123'
LIMIT 20;
I've tried with scope:
#already have my restaurant resource here with id 1
#restaurant.check_items.by_item_category params[:category_uuid]
And in my models I would have:
class CheckItem < ActiveRecord::Base
...
scope :by_item_category, -> value { joins(:item).by_category value }
class Item < ActiveRecord::Base
...
scope :by_category, -> value { joins(:category).where('%s.uuid = ?' % Category.table_name, value)}
Buut this doesn't seem to work
This appears to be the only way I found this to be working if anyone is interested.
CheckItem.joins(:item => {:category => :restaurant}).where('category.uuid=? and restaurant.id=?', 123123, 1)

Resources