Trying to model the relationship between two people in rails - ruby-on-rails-4

I have the following two simple classes:
class Person < ActiveRecord::Base
has_many :parent_child_assignments
has_many :children, :through => :parent_child_assignments
has_many :parents, :through => :parent_child_assignments
end
class ParentChildAssignment < ActiveRecord::Base
belongs_to :parent, :class_name => "Person"
belongs_to :child, :class_name => "Person"
end
created from the following rails commands
rails generate model Person name:string
rails generate model ParentChildAssignment parent_id:integer child_id:integer
I can get one direction (joe.children or joe.parents) to work but not both
Someone must have asked this question before, but I can't find it.
Thanks in advance.

The classes should look like:
class Person < ActiveRecord::Base
has_many :parent_child_assignments_as_parent, class_name: 'ParentChildAssignment', :foreign_key => :parent_id
has_many :parent_child_assignments_as_child, class_name: 'ParentChildAssignment', :foreign_key => :child_id
has_many :children, :through => :parent_child_assignments_as_parent
has_many :parents, :through => :parent_child_assignments_as_child
end
class ParentChildAssignment < ActiveRecord::Base
belongs_to :parent, :class_name => "Person", :foreign_key => :parent_id
belongs_to :child, :class_name => "Person", :foreign_key => :child_id
end

Related

Rails includes with has_many through giving avoid eger loading n+1 by bullet gem

My Models are like:
class ThreeDModel < ApplicationRecord
has_many :three_d_model_animations
has_many :animations, through: :three_d_model_animations
has_many :three_d_model_images, dependent: :destroy
has_many :three_d_garments
has_one :model_efm
has_one :model_edm
end
2nd:
class ThreeDModelAnimation < ApplicationRecord
belongs_to :animation
belongs_to :three_d_model
validates_presence_of :animation_file
#validates_presence_of :motion
mount_uploader :animation_file, ThreeDModelAnimationUploader
enum animation_motions: {
'Run': 'run',
'Turn Aroun (100 Frames)': 'turn_around_100_frames',
'Turn Around (300 Frames)': 'turn_around_300_frames',
'Walk (58 Frames)': 'walk_58_frames',
'Walk (92 Frames)': 'walk_92_frames'
}
end
3rd:
class Animation < ApplicationRecord
has_many :three_d_model_animations
has_many :three_d_models, through: :three_d_model_animations
mount_uploader :video, AnimationVideoUploader
validates_presence_of :video
validates :name, :frames, :loop_start, :loop_end, presence: true
end
Now my query is:
#model = ThreeDModel.where(id: params[:id]).includes(:model_efm, :model_edm, :three_d_model_images, :three_d_model_animations, :animations).last
which gives the following message by the bullet gem:
AVOID eager loading detected
ThreeDModel => [:three_d_model_animations, :animations]
is there a better way so that n+1 could be avoided. Thanks in advance

Nested form has_many through issue

I have two models which are related with has_many :through association.
I have user & software_license models associated through user_software_license model. I am trying to save data using nested form, but I am getting errors
my models :
user.rb
has_many :software_licenses, through: :user_software_licenses
has_many :user_software_licenses
accepts_nested_attributes_for :software_licenses, :allow_destroy => true
software_license.rb
has_many :users, through: :user_software_licenses
has_many :user_software_licenses
accepts_nested_attributes_for :user_software_licenses
user_software_license.rb
belongs_to :user
belongs_to :software_license
my nested form :
= f.fields_for :software_licenses do |software_license|
= render :partial => "users/software_licenses", :locals => {:f => software_license}
= link_to_add_fields "Add", f, :user_software_licenses, true, "users/software_licenses"
this is how I am initialising it in my controller
user.software_licenses.build
and when I click on 'add more' button in nested form after submitting form I am getting parameters like this :
"software_licenses_attributes"=>{"0"=>{"id"=>"2"}},
"user_software_licenses"=>{"id"=>"2"},

Link models through acts_as_taggable_on rails

I have a model Product which has_many :taxons, through: :classifications. How to implement acts_as_taggable_on to implement the tagging of products with the names of taxons instead of the the tags from the tag table? Is it possible or not?
#app/models/product.rb
class Product < ActiveRecord::Base
translates :name
accepts_nested_attributes_for :translations, allow_destroy: true, reject_if: :all_blank
# Associations
has_many :classifications, inverse_of: :product, dependent: :delete_all
has_many :taxons, through: :classifications
end
#app/models/classification.rb
class Classification < ActiveRecord::Base
self.table_name = 'products_taxons'
# Associations
belongs_to :product, inverse_of: :classifications
belongs_to :taxon, inverse_of: :classifications, touch: true
end
#app/models/taxon.rb
class Taxon < ActiveRecord::Base
translates :name
globalize_accessors
has_many :classifications, inverse_of: :taxon, dependent: :delete_all
has_many :products, through: :classifications
end

(rails4)how can i validate an user cannot book a route with overlapping time?

i have an user model:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :password, :password_confirmation, :cardnumber, :registration
validates_uniqueness_of :email
has_many :bookings
has_many :routes, :through => :bookings
end
then a route model :
class Route < ActiveRecord::Base
attr_accessible :id, :departurecity, :arrivalcity, :departuretime, :arrivaltime, :train_id
has_many :stops
belongs_to :train
has_many :bookings
has_many :users, :through => :bookings
end
an finally bookink model:
class Booking < ActiveRecord::Base
attr_accessible :route_id, :user_id, :dateb, :hourb, :citydeparture, :cityarrival, :id, :tripclass
belongs_to :user
belongs_to :route
The question is how can I make a method on booking model which an user cannot book a train route with overlap time on another booked train route by the same user?

Unknown validator location validator

I am working on my very first rails app so please pardon me if the answer is obvious.
My userlocation model looks like this
class UserLocation < ActiveRecord::Base
belongs_to :uid, :class_name => User, :foreign_key => "uid"
has_one :current_location, :class_name => Location, :foreign_key => "city"
has_one :destination, :class_name => Location, :foreign_key => "city"
validates :user, presence: true
end
When I try to put something into this model I get the said error. What am I doing wrong?
I think the class_name option should be a string, not a class.
Try using :class_name => "User" and :class_name => "Location" in your associations.
See examples on http://guides.rubyonrails.org/association_basics.html