Unknown key find_options during database migration - ruby-on-rails

I'm trying to update an old Redmine plugin but when I try to do the migration I get this error. Could someone give me some pointers how to address the problem?
I tried to replace find_options with scope but I'm not exactly sure how do to it.
rake aborted!
ArgumentError: Unknown key: :find_options. Valid keys are: :type, :permission, :timestamp, :author_key, :scope
/home/developer/projects/redmine/redmine-3.3.1/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb:32:in `acts_as_activity_provider'
Migration file:
require File.join(File.dirname(__FILE__), '../../app/models', 'hudson_build')
class UpdateBuilding < ActiveRecord::Migration
def self.up
HudsonBuild.update_all "building = 'true'", "building = 't'"
HudsonBuild.update_all "building = 'false'", "building = 'f'"
end
def self.down
HudsonBuild.update_all "building = 't'", "building = 'true'"
HudsonBuild.update_all "building = 'f'", "building = 'false'"
end
end
part of my hudson_build.rb model that's causing the problem:
require 'hudson_api_error'
require 'hudson_exceptions'
require 'rexml_helper'
include RexmlHelper
class HudsonBuild < ActiveRecord::Base
unloadable
has_many :changesets, :class_name => 'HudsonBuildChangeset', :dependent => :destroy
has_one :test_result, :class_name => 'HudsonBuildTestResult', :dependent => :destroy
has_many :artifacts, :class_name => 'HudsonBuildArtifact', :dependent => :destroy
belongs_to :job, :class_name => 'HudsonJob', :foreign_key => 'hudson_job_id'
belongs_to :author, :class_name => 'User', :foreign_key => 'caused_by'
validates_presence_of :hudson_job_id, :number
validates_uniqueness_of :number, :scope => :hudson_job_id
acts_as_event :title => Proc.new {|o|
retval = "#{l(:label_build)} #{o.job.name} #{o.number}: #{o.result}" unless o.building?
retval = "#{l(:label_build)} #{o.job.name} #{o.number}: #{l(:notice_building)}" if o.building?
retval
},
:description => Proc.new{|o|
items = []
items << o.test_result.description_for_activity if o.test_result != nil
items << HudsonBuildChangeset.description_for_activity(o.changesets) if o.changesets.length > 0
items.join("; ")
},
:datetime => :finished_at
acts_as_activity_provider :type => 'hudson',
:timestamp => "#{HudsonBuild.table_name}.finished_at",
:author_key => "#{HudsonBuild.table_name}.caused_by",
:find_options => {:include => {:job => :project}},
:permission => :view_hudson
include HudsonHelper
extend RexmlHelper

I don't know this plugin but i found this commit
Please try changing in HudsonBuild class this line:
:find_options => {:include => {:job => :project}},
to
:scope => includes(:project),
Here is an example.

Related

How to create a record through, has many association with two foreign keys.

I need to create a route which is associated to a location.
class Location < ActiveRecord::Base
has_many :routes,:foreign_key => "origin_id"
end
and a route belongs to different locations:
class Route < ActiveRecord::Base
belongs_to :origin, :class_name => 'Location', :foreign_key => 'location_id'
belongs_to :destination, :class_name => 'Location', :foreign_key => 'destination_id'
validates :origin, :presence => {:message => 'origin cannot be blank'}
end
location.routes.create({
:destination => resort_location,
:destination_id => resort_location.id,
:total_distance => body['route_summary']['total_distance'],
:total_time => body['route_summary']['total_time'],
:raw => response.body
})
Records get create without origin and destination, even though I do have the presence validation.
Thanks for helping me out.
Code looks okay to me -
location.routes.create({
:destination => resort_location, #-> where is resort_location defined?
:destination_id => resort_location.id,
:total_distance => body['route_summary']['total_distance'],
:total_time => body['route_summary']['total_time'],
:raw => response.body
})
Your validates method also needs to be for a specific attribute (not a relation). In your case, it should be like this:
validates :location_id, presence: {message: 'You need an origin!'}
Do you have any errors / logs for this?

after_save error in ruby on rails

I have two models linked to each other and I am trying to do an after_save, create in the model and code is as follows.
class CustomerBill < ActiveRecord::Base
after_save :updating_and_creating_ledger_items
has_many :customer_ledgers, :dependent => :destroy
def updating_and_creating_ledger_items
CustomerLedger.create(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
end
end
customer ledger model
class CustomerLedger < ActiveRecord::Base
belongs_to :customer_bill, :foreign_key => "customer_bill_id"
end
Now the problem is the program executes perfectly but the value are not been put in the database. If I check Customer ledger it is still empty.
The values are not getting stored. what seems to be the problem? Guidance towards this matter will be helpful.
Thanks in advance. :)
Add
validates_associated :customer_ledgers
In customer_bill.rb
Try
ledger = customer_ledgers.build(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
ledger.save
EDITED for to avoid Validations, use
ledger = customer_ledgers.build(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
ledger.save(:validate => false)
The CustomerLedger may be failing a validation check. Replace:
def updating_and_creating_ledger_items
CustomerLedger.create(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
end
with:
def updating_and_creating_ledger_items
ledger = customer_ledgers.build(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
ledger.save(:validate => false)
end
You're not setting the customer_bill_id in the create, change create to build as so:
customer_ledger.build(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)

Rails has_many build method as a polymorphic resource

class OrganizationModuleAttachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end
class Document < ActiveRecord::Base
has_many :organization_module_attachments, :as => :attachable, :dependent => :destroy
def organization_module_attachment_ids=(values)
(values || []).each_with_index do |organization_module_id, index|
organization_module_attachments.build(:organization_module_id => organization_module_id, :attachable_type => "Document", :position => index + 1)
end
end
end
form.html.haml:
= f.select :organization_module_attachment_ids, options_for_select((#organization_modules || []).collect { |a| [a.name, a.id] }, (#document.organization_modules || []).collect { |a| a.id }), {}, { :class => "multiselect", :multiple => true }
So in the document class I am trying to build organization_module_attachments. I get an error in the creation of the organization module attachment when I submit the form. I think rails is assuming the foreign key on an attachment is :document_id, when in fact it is polymorphic and is therefore :attachable_id. If I explicitly set the :attachable_id in the build method it works fine.
I've tried a number of things and searched around for a few days with no luck. Does anyone know how to do this?

Updating object with belongs_to associations and nested_attributes

I've got problems with making update action for one of my data objects. I've got:
class UserProfile < ActiveRecord::Base
belongs_to :address, :dependent => :destroy
belongs_to :post_address, :class_name => 'Address', :dependent => :destroy
accepts_nested_attributes_for :address
accepts_nested_attributes_for :post_address
# validations and stuff
end
class Address < ActiveRecord::Base
# validations and stuff
end
And the problem is with the form and action:
= form_for #up, :url => '/profile/edit', :method => :post do |f|
= f.error_messages
#...
= f.fields_for :address, #up.address do |a|
#...
= f.fields_for :post_address, #up.post_address do |a|
#...
.field.push
= f.submit 'Save', :class=>'ok'
Action:
def edit_account
#user = current_user
if request.post?
#up = #user.user_profile.update_attributes(params[:user_profile])
if #up.save
redirect_to '/profile/data', :notice => 'Zmiana danych przebiegła pomyślnie.'
end
else
#up = #user.user_profile
end
end
The error I get looks like this:
Couldn't find Address with ID=3 for UserProfile with ID=2
And it occurs in the line:
#up = #user.user_profile.update_attributes(params[:user_profile])
I think that AR tries to create another Address when the form is submitted but I'm not certain.
Why do I get this error? What's wrong with my code?
So not sure how that works on new since #up.address is nil. Can you try something like:
=f.fields_for :address, (#up.address.nil? ? Address.new() : #up.address) do |a|
#...
= f.fields_for :post_address, (#up.post_address.nil? Address.new() : #up.post_address) do |a|
#...
That might make a difference?
Solved
I just changed the type of association in UserProfile:
has_one :address,
:class_name => 'Address',
:foreign_key => 'user_profile_id',
:conditions => {:is_post => false},
:dependent => :destroy
has_one :post_address,
:class_name => 'Address',
:foreign_key => 'user_profile_id',
:conditions => {:is_post => true},
:dependent => :destroy,
:validate => false
And slightly adjusted the controller. Thanks for help!

rails accepts_nested_attributes_for and validations... Rails 2.3.11

I have two models
class Group < AR
has_many :permissions
accepts_nested_attributes_for :permissions, :allow_destroy => true
end
class Permission < AR
validates_uniqueness_of :action, :scope => [:role]
end
I can't seem to get the unique constraint on permissions to work when creating a new group, only on update. Here's a sample output. Does anyone know the best way to get validation to work on nested attributes and unique constraints?
sample output
> g = Group.create(:permissions_attributes => [{:role => 'admin', :action => 'one'}])
> # Now add the same permissions, should not be valid
> g.permissions_attributes = [{:role => 'admin', :action => 'one'}]
> g.valid? # => false
That is expected. However if I create the Group with the same permissions_attributes twice, it doesn't invalidate:
> g = Group.new(:permissions_attributes => [{:role => 'admin', :action => 'one'}, {:role => 'admin', :action => 'one'}]
> g.valid? # => true BUT THIS SHOULD BE FALSE!!
> g.save # => true Oh Nos!!!
class Group < AR
has_many :permissions
accepts_nested_attributes_for :permissions, :allow_destroy => true
validates_associated :permissions
end

Resources