adding some parent CRUD
This commit is contained in:
@ -5,6 +5,17 @@ class Child < ActiveRecord::Base
|
||||
validates :first_name, presence: true
|
||||
validates :last_name, presence: true
|
||||
|
||||
scope :not_related_to, lambda { |parent_id|
|
||||
joins("LEFT JOIN (
|
||||
SELECT child_id
|
||||
FROM parenthoods
|
||||
WHERE person_id = #{sanitize(parent_id)}
|
||||
) as s1 on s1.child_id = children.id")
|
||||
.where("s1.child_id is null")
|
||||
.order(:first_name, :last_name)
|
||||
.uniq
|
||||
}
|
||||
|
||||
def name
|
||||
"#{first_name} #{last_name}"
|
||||
end
|
||||
|
@ -1,10 +1,11 @@
|
||||
class Person < ActiveRecord::Base
|
||||
has_many :parenthoods
|
||||
has_many :children, through: :parenthoods
|
||||
accepts_nested_attributes_for :children
|
||||
accepts_nested_attributes_for :children, reject_if: :all_blank
|
||||
|
||||
validates :first_name, presence: true
|
||||
validates :last_name, presence: true
|
||||
validates :phone, presence: true
|
||||
|
||||
scope :with_name, lambda { |name|
|
||||
where("concat(first_name, ' ', last_name) RLIKE ?", name)
|
||||
|
Reference in New Issue
Block a user