Try this:
class ObjectivesController < ApplicationController
before_filter :require_user
layout false
def create
objective = current_user.objectives.create(params[:objective])
end
end
And as others have said, don't overwrite AR class methods like
'create', otherwise you will enter a world of pain.
Also, you are deleting a param called 'freq_options', if the model
does not need it then it shouldn't be in the hash. If the controller
needs it, then pass it as params[:freq_options] rather than params
[:objective][:freq_options]. If it's not used there, then why pass it
at all?
-- W
On Oct 24, 11:31 am, drewB <[email protected]> wrote:
> I am running into a very odd problem. I have an ActiveRecord called
> Objective with a public create method. When this method is called in
> a controller the return value is a Hash instead of Objective. When I
> test the method in Rspec for the model it returns as Objective.
>
> Does anyone have an idea about why the return type would be different
> and if so how to fix it so it always returns Objective?
>
> Code:
>
> class Objective < ActiveRecord::Base
> belongs_to :user
> has_many :next_actions, :order => 'id DESC', :conditions =>
> {:completed => false}
> has_many :action_items, :order => 'position'
> has_many :progress_updates
>
> serialize :freq_value
>
> validates_presence_of :name, :due_date, :completion, :pr_u_frequency_id
> validates_numericality_of :completion, :only_integer =>
> true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100
>
> def self.create(attributes, user_id)
> #create new record with user_id correctly set
> attributes.delete :freq_options #deletes this unused key
> obj = self.new(attributes)
> obj.user_id = user_id
> obj.save ? obj : nil
>
> end
>
> end
>
> class ObjectivesController < ApplicationController
> before_filter :require_user
>
> layout false
>
> def create
> objective = Objective.create(params[:objective], current_user.id)
> Rails.logger.info("****objective.class = #{objective.class}****")
> end
> end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---