Here's the (simplified) code:
class Biosimilar::AdverseEvent < ActiveRecord::Base
attr_accessibile :adverse_event_med_conds_attributes
has_many :adverse_event_med_conds,
:class_name => 'Biosimilar::AdverseEventMedCond',
:dependent => :destroy
has_many :med_conds,
:class_name => 'Biosimilar::MedCond',
:through => :adverse_event_med_conds
accepts_nested_attributes_for :adverse_event_med_conds,
:allow_destroy => true,
:reject_if => proc { |attributes| attributes.any? {|k,v| v.blank?} }
end
When the form is submitted, the record is created on "adverse_event_med_conds" table even if the user leaves the "med_cond_id" field EMPTY. The reject_if doesn't work!
Any suggestions?
Ok, forget it. The above code is correct! The problem was in the controller, I accidentally left in the "show" method the following lines:
if #adverse_event.adverse_event_med_conds.size == 0
#adverse_event.adverse_event_med_conds.build
end
...and the "build" call was the one that caused record creation.
Related
app/models/donor.rb
has_many :donor_relationships
accepts_nested_attributes_for :donor_relationships, :allow_destroy => true
app/models/donor_relationship.rb
belongs_to :donor, optional: true
I am using f.fields_for in donor form and creating donor and donor_relationships both.
donor_relationships is not required must. Issue I face is that when if I not add any donor_relationships then empty record of donor_relationship is created with donor id. In rails 4 not like this happen.
How can I fix this?
accepts_nested_attributes_for ignore blank values
You can add a reject_if conditional to the accepts_nested_attributes method. Assuming your donor_relationship has an attribute of name (you can go with relationship_id or whatever attribute makes sense):
accepts_nested_attributes_for :donor_relationships,
:allow_destroy => true,
:reject_if => lambda { |c| c[:name].blank? }`
You can use reject_if option:
accepts_nested_attributes_for :donor_relationships,
allow_destroy: true,
reject_if: proc { |attributes| attributes['important_field'].blank? }
An element can be associated with different models through the polymorphic association elementable. Because I want to use nested forms, I have to make associations for the different models explicit (association element_recommendation).
The following code works as intended:
class Element < ActiveRecord::Base
belongs_to :elementable, :polymorphic => true, :dependent => :destroy
belongs_to :element_recommendation, ->(element) {
if element.elementable_type == 'ElementRecommendation'
where('true = true')
else
none
end }, :class_name => "ElementRecommendation", :foreign_key => "elementable_id"
[..]
But I'm unhappy with the lambda in the element_recommendation association. It's an all or nothing association. The none-part is cognizable, but the take it as it is-part is not obvious. How can I make where('true = true') recognizable?
I've found the solution while I was writing the question:
belongs_to :element_recommendation, ->(element) {
if element.elementable_type == 'ElementRecommendation'
self
else
none
end }, :class_name => "ElementRecommendation", :foreign_key => "elementable_id"
This is a rather simple Rails 4 situation. Model Intranet has_many activities. Activities exists with sufficient records for several intranets. Current_intranet.activities.size returns 69 records. However, whenever I try to access any of the records, I receive "output error: <ArgumentError: wrong number of arguments (1 for 0)" even though I'm not passing any arguments. All of the following fail with that error.
Current_intranet.activities.first
Activity.where(intranet_id: 1).first
Activity.where(:intranet_id => 1).first
Activity.where{intranet_id.eq 1}.first
All of the above with [0] instead of first
There is no problem with any other models
I'd appreciate any suggestions. Thanks in advance.
There are no defined scopes. The code is:
class Activity < ActiveRecord::Base
acts_as_capitalized
# activities created during intranet creation.
DEFAULT_ACTIVITIES = { 'Document Preparation' => 'general income', 'No Charge' => 'general income'}
# static activity names that can not be deleted.
STATIC_ACTIVITY_NAMES = [ 'Document Preparation','No Charge']
before_destroy :check_if_used
before_destroy :cannot_delete_static_activities
belongs_to :created_by_user, :class_name => "User", :foreign_key => "created_by"
belongs_to :updated_by_user, :class_name => "User", :foreign_key => "updated_by"
belongs_to :intranet
belongs_to :chart_of_account
has_many :activity_rates, :dependent => :destroy
has_many :event_type
has_many :billings do
def find_by_name(name)
(find :all, :conditions => ["activity.name = ?",name])
end
end
has_many :document_templates, :foreign_key => "billing_activity_id"
validates_presence_of :intranet_id, :name, :chart_of_account_id
validates_uniqueness_of :name, :scope => :intranet_id
Is there any way to halt saving of child before parent.
I am using accepts_nested_attributes_for with polymorphic association.
I used multiple options validates_presence_of :parent_id , validates_assoicated :parent but none are working.
For example, I do have a class
Class Person
include HasPhoneNumbers
..
end
module HasPhoneNumbers
def self.included(kclass)
kclass.has_many :phone_numbers, :as => :callable, :dependent => kclass == Person ? :destroy : :nullify
end
klass.accepts_nested_attributes_for :phone_numbers, :reject_if => lambda {|pn| pn.keys.any?{|k| k.to_sym != :id && pn[k].blank?} }
end
class PhoneNumber
belongs_to :callable, :polymorphic => true
end
So while saving person due to validation in person object, it was not saving. However, child(phone_number) was saving. So I need to restrict it to not save child(phone_number) before parent(person) saves.
I did try multiple options using validates_presence_of and validates_associated, but none are working for me.
#person = Person.new(params[:person])
ActiveRecord::Base.transaction do
person.save!
end
Wrapping your saves within a transaction should roll back the phone number save if the person fails validation.
Reference: ActiveRecord Transactions
I've been using the association_collection method "other_ids" throughout my Rails app with no issues. However whenever I try to access it from within the model defining the association, Rails has no idea what I'm taking about. For example:
class Membership < ActiveRecord::Base
belongs_to :course, :touch => true
belongs_to :person, :touch => true
end
class Day < ActiveRecord::Base
belongs_to :course, :touch => true, :counter_cache => true
has_many :presents, :dependent => :delete_all
has_many :people, :through => :presents
before_destroy :clear_attendance
def clear_attendance
mems = Membership.where(:course_id => course.id, :person_id => person_ids)
mems.update_all(["attendance = attendance - ?", (1 / course.days.size.to_f)])
end
end
In this case, person_ids is always null. I've tried self.person_ids, people.ids, etc. All nothing. I have used day.person_ids elsewhere with no issues, so why can't I use it here?
I am using Ruby 1.9.1 and Rails 3.0.3. Here is the SQL call from my log:
[1m[36mAREL (0.0ms)[0m [1mUPDATE "memberships" SET attendance = attendance - 0.3333333333333333 WHERE ("memberships"."course_id" = 4) AND ("memberships"."person_id" IN (NULL))[0m
edit: added more code to clarify question
What you really want there is:
def a_method
self.people.all
end
But to answer your question, person_ids is the correct method, and it should return an empty array, not nil. I just tried an association like that out in 2.3.10. Maybe you can post some more of your code, rails version, etc.
Thanks for your help - I figured it out myself. The problem was the order of my callbacks. I was trying to call person_ids after the association had been deleted. Changing the order to this solved my issues.
class Day < ActiveRecord::Base
before_destroy :clear_attendance
belongs_to :course, :touch => true, :counter_cache => true
has_many :presents, :dependent => :delete_all
has_many :people, :through => :presents