Here is my three models/tables
class Swimming::Classschedule < ActiveRecord::Base
belongs_to :swimming_classtimes ,:class_name=>'Swimming::Classtime',:foreign_key => "classtime_id"
attr_accessible :id,:coach_id, :level_id, :note, :classtime_id
end
class Swimming::Classtime < ActiveRecord::Base
has_many :swimming_classschedules,:class_name=>'Swimming::Classschedule'
belongs_to :swimming_timeblocks ,:class_name=>'Swimming::Timeblock',:foreign_key => "timeblock_id"
attr_accessible :date, :end, :start,:timeblock_id,:id
end
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
attr_accessible :name,:id
end
swimming_classschedules belongs_to swimming_classtimes
swimming_classtimes belongs_to swimming_timeblocks
swimming_timeblocks has_many swimming_classtimes
swimming_classtimes has_many swimming_classschedules
How are swimming_classschedules and swimming_timeblocks associated ?
swimming_timeblocks has_many swimming_classschedules through swimming_classtimes
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
has_many :swimming_classschedules,:through => :swimming_classtimes
attr_accessible :name,:id
end
Related
I have an application with a business logic concerning products with multiple variants:
class Task < ApplicationRecord
belongs_to :variant
end
class Variant < ApplicationRecord
belongs_to :product
has_many :variant_option_values
has_many :option_values, through: :variant_option_values
has_many :prices
end
class Product < ApplicationRecord
has_many :product_option_types
has_many :option_types, through: :product_option_types
has_many :variants
end
class OptionValue < ApplicationRecord
belongs_to :option_type
end
class OptionType < ApplicationRecord
has_many :product_option_types
has_many :products, through: :product_option_types
has_many :option_values
end
class ProductOptionType < ApplicationRecord
belongs_to :product
belongs_to :option_type
end
class VariantOptionValue < ApplicationRecord
belongs_to :variant
belongs_to :option_value
end
The ERD looks like this:
Having a product product_1 how can I find its variants that have OptionValue instances option_value_1, option_value_2 and option_value_3? Note that the variant has to have all three option values at the same time and can have more than those three (but not necessarily).
option_values = [option_value_1, option_value_2, option_value_3]
Variant.include(product: [option_types: :option_values])
.where("option_values.id IN (?)", option_values.map(&:id))
.group("products.id")
.having("count(*) >= ?", option_values.size)
I am attempting to do a query via a junction table, though Rails is given me the below error
Venue Model
class Venue < ActiveRecord::Base
attr_accessible :address, :latitude, :longitude, :name, :phone, :suburb, :state, :country
after_validation :geocode
has_many :orders, through: :venues_orders
geocoded_by :full_address
def full_address
[address, suburb, state, country].compact.join(', ')
end
end
Order Model
class Order < ActiveRecord::Base
attr_accessible :fulfilled, :item, :placed, :person_id, :special_instructions, :priority, :flag, :milk
belongs_to :person
belongs_to :venue
Venues Orders Model
class VenuesOrders < ActiveRecord::Base
attr_accessible :order_id, :venue_id
end
class Venue < ActiveRecord::Base
has_many :orders, through: :venues_orders
has_many :venues_orders
end
class Order < ActiveRecord::Base
has_many :venues, through: :venues_orders
has_many :venues_orders
end
class VenuesOrders < ActiveRecord::Base
belongs_to :venue
belongs_to :order
end
For more details read : RailsGuides
Another advice: The convention for creating a join table is lexical ordering. Like, OrdersVenues not VenuesOrders
I have these four models:
class Workgroup < ActiveRecord::Base
has_many :empgroups
has_many :employees, through: :empgroups
has_many :workorders
class Empgroup < ActiveRecord::Base
attr_accessible :employee_id, :workgroup_id
belongs_to :employee
belongs_to :workgroup
class Employee < ActiveRecord::Base
has_many :empgroups
has_many :workgroups, through: :empgroups
has_many :workorders
class Workorder < ActiveRecord::Base
belongs_to :employee
belongs_to :workgroup
When a new workorder is created, I want to validate that the employee belongs to the selected workgroup.
How can I code that validation in the workorder model?
Thanks for the help!
I would write a custom validation method in the workorder model. So it might look something like this.
class Workorder < ActiveRecord::Base
belongs_to :employee
belongs_to :workgroup
validate :check_employee_workgroup
private
def check_employee_workgroup
errors.add(:employee_id, "Employee is not available in this work group") unless
self.employee.workgroups.select(:id).where(id: [self.workgroup_id]).exists?
end
end
The need to do this may be a design issue. Perhaps you should revise your classes to look something like this:
class Workgroup < ActiveRecord::Base
has_many :empgroups
has_many :employees, through: :empgroups
class Empgroup < ActiveRecord::Base
attr_accessible :employee_id, :workgroup_id
belongs_to :employee
belongs_to :workgroup
class Employee < ActiveRecord::Base
has_many :empgroups
has_many :workgroups, through: :empgroups
class Workorder < ActiveRecord::Base
belongs_to :empgroup
This way, you don't need a custom validator at all. From there, you can add in any other through: relationships necessary
I have these models and associations. I want to reach roleable trough privilege model doesnt matter what roleable_type is(Dj or Photographer)? Use join model because privilege model will have other attributes. It is possible something like this:
class User
has_one :privilege, dependent: :destroy
has_one :roleable, through: :privilege
end
class Dj < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Photographer < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Privilege < ActiveRecord::Base
belongs_to :user
belongs_to :roleable, polymorphic: true
end
If i add source_type: 'Dj' to has_many :through return only with roleable_type 'Dj'. I want to do this bellow:
u = User.first
u.roleable #return privilage roleable( doesnt matter Dj or Photograher)
I'd make those belongs_to, not that that changes anything.
class User < ActiveRecord::Base
has_one :privilege, dependent: :destroy
has_one :roleable, through: :privilege
end
class Dj < ActiveRecord::Base
has_one :privilege
belongs_to :user, through: :privilege, as: :roleable
end
class Photographer < ActiveRecord::Base
has_one :privilege
belongs_to :user, through: :privilege, as: :roleable
end
class Privilege < ActiveRecord::Base
belongs_to :user
belongs_to :roleable, polymorphic: true
end
Can you post, what u.roleable returns?
I have these models
class EventGroups < ActiveRecord::Base
has_many :festival_venues
has_many :venues, :through => :festival_venues
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now I want to create a Venue via Eventgroups, and record in the FestivalVenue should be created as well.
When i delete Eventgroups related record in Venue and
FestivalVenue should be deleted as well.
How can i do this?
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now if you have event_group variable bound to EventGroup object, you create Venue (along with its FestivalVenue) with:
venue = Venue.create(your_attributes)
event_group.venues << venue
In your below code, Model class name must be singular. Change class name EventGroups to EventGroup. Now it will work like a charm.
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
Remaining code is good.
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Hope it will help. Thanks