#<ActiveRecord::Associations::CollectionProxy []> in Rails - ruby-on-rails

I am facing this error while running my application:
<ActiveRecord::Associations::CollectionProxy []>
I am able to store reports but couldn't store icons. It is storing fine in Rails 3.2.13, but raising this issue in Rails 4.2.6.
report.rb:
class Report < ActiveRecord::Base
belongs_to :user
has_many :icons, -> { order 'position_id ASC'}
accepts_nested_attributes_for :icons, :reject_if => lambda { |a| a[:icon].blank? }, :allow_destroy => true
end
icon.rb:
class Icon < ActiveRecord::Base
belongs_to :report
end
reports_controller:
def new
#report = #user.reports.new({
:background_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.background,
:text_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.commentary,
:button_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.button
})
3.times { #report.icons.build }
end
def create
respond_to do |format|
if #report.save
format.json { render :json => { :success => true, :user_id => #user.id, :report_id => #report.id, :report_title => #report.title, :icon_array => #report.icons, :redirect => user_report_url(current_user, #report.id) } }
else
format.json { render :json => { :success => false } }
end
end
end
I am able to store reports but icons are not stored. Please help

I think you might have missed icon attributes in the strong parameters.

Related

Filter with where inside render json

I have this in a controller:
def get_tags
person = Person.where({fuid: params[:fuid]})
render json: person, :only=>[:fuid], :include => [
:hashtags => {
:except => [:created_at, :updated_at],
:methods => :person_count
}
]
end
I am including hashtags but I need to filter those hashtags for only the ones that have active = 1. Is there a way to do this?
You could do this in the model:
class Person < ActiveRecord::Base
has_many :active_hashtags,-> { where active: 1 }, class_name: 'Hashtags'
end
Then in your controller:
render json: person :only=>[:fuid], :include => [
:active_hashtags => {
:except => [:created_at, :updated_at],
:methods => :person_count
}
]

possible to nest method calls on to_json

I'm trying to do something like this:
render :json => r.to_json(:methods => [:food_item => {:method => :price_value}])
but it's not working. Is something like this even possible?
thx
edit 1
no association
def food_item
MenuItem.find(food_id)
end
Is food_item an ActiveRecord association? If so, you could try
render :json => r.to_json(:include => { :food_item => { :only => :price_value } })
I'll refine my answer in response to "edit 1". First, remove your food_item method and add an actual association like this:
belongs_to :food_item, :class_name => "MenuItem", :foreign_key => "food_id"
and then do
render :json => r.to_json(:include => { :food_item => { :only => [:price_value] } })

Acts_as_paranoid and validation

Using rails 3 and acts_as_paranoid. I want to make sure that every task has a comment.
How can I bypass this one validation (check_if_notes) if mark_completed_and_msg is called?
EDIT - Is this the right way?
task.rb
has_one :comment, :as => :commentable
attr_accessor :force_task
# original was - before_update, :check_if_notes
validate :check_if_notes, :on => :update, :unless => proc { |a| a.force_task }
def mark_completed_and_msg(user_id, msg)
Comment.create!(:commentable_type => self.class, :commentable_id => self.id, :content => msg )
self.update_attributes(:completed_by => user_id, :deleted_at => Time.now, :force_task => true)
end
def check_if_notes
if self.comment.blank? || self.comment.content.blank? || self.comment.content.scan(/[\w-]+/).size <= 2
saved = false
self.comment.errors[:content] << "You must have a comment and more than 3 words."
raise ActiveRecord::Rollback
end
end
I think you meant using == in your Proc.
:unless => proc { |a| a[:force_task] == true }
You can also use
:unless => proc { |a| a.force_task? }

include 2nd level in to_json

I'm using this code to convert a model to json. If i try to use an include 2nd level like this:
p = Product.includes({ :variants => { :stocks => :size } }).where(:id => params[:id]).first
render :json => p.variants.to_json(:include => { :stocks => { :include => :size } })
I receive this error:
undefined method `macro' for nil:NilClass
How I can solve that?
Try this:
render :json => p.variants.map { |v| v.as_json(:include => {:stocks => {:include => :size}}) }
Info about Object#as_json/to_json here.

Adding an rails activerecord association within a loop

I want to add a has_many through association to a activerecord model class for each symbol in an array. for example
PeopleOrganisation::ROLES.each do |role|
has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
:conditions => "people_organisations.role = '#{role.to_s}'" do
def << (object)
PeopleOrganisation.send(:with_scope, :create => {:role => **role**}) { self.concat object }
end
end
end
everything works fine except for the reference to the role variable inside the method def. This is because the method def is not a closure. Is there a way of achieving what I want?
Try this:
PeopleOrganisation::ROLES.each do |role|
has_many(role.to_s.pluralize.to_sym,
:through => :people_organisations, :source => :person,
:conditions => ["people_organisations.role = ?", role]
) do
define_method("<<") do |object|
PeopleOrganisation.send(:with_scope, :create => {:role => role}) {
self.concat object
}
end
end
end
Instead of defining method using def you can try define_method method:
PeopleOrganisation::ROLES.each do |role|
has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
:conditions => "people_organisations.role = '#{role.to_s}'" do
define_method(:<<) do |object|
PeopleOrganisation.send(:with_scope, :create => {:role => role}) { self.concat object }
end
end
end

Resources