On 21 Oct 2008, at 18:44, Chris Bloom wrote:

>
> Mark Reginald James wrote:
>> Nate wrote:
>>
>>>
>>>   class Revision < ActiveRecord::Base
>>>        belongs_to :environment
>>>        belongs_to :source_revision,
>>>             :class_name => 'Revision',
>>>             :foreign_key => 'source_revision_id'
>>>   end
>>
>> In your Revision model are you making any assignments to an  
>> @environment
>> instance variable, or have you defined an "environment"  
>> attr_accessor?
>> If so, rename to avoid conflict with the belongs_to.
>>
>> --
>> We develop, watch us RoR, in numbers too big to ignore.
>
> Is there a way to do this and have it use the association? I need to
> override the assoc= method of a model to make sure the assignment
> matches some predefined criteria. However, if I try to assign via
> @assoc= I get the error message about an undefined target, and if I  
> use
> self.assoc= I wind up in an endless recursion until a stack overflow
> occurs.
>
> class Project < ActiveRecord::Base
>  belongs_to :status, :class_name => "ProjectStatus",
>    :foreign_key => 'status_code'
>
>  # Updates the current project's status unless the current
>  # status is greater than the new status. Status' are compared
>  # using ProjectStatus::compare_status method The
>  # new_project_status argument should be a ProjectStatus record
>  # (i.e. ProjectStatus::SOME_STATUS)
>  def status= (new_project_status, override = false)
>    puts "Current status = [EMAIL PROTECTED]" #Debugging
>    if override or @status < new_project_status
>      puts "New status = #{new_project_status}" #Debugging
>      @status = new_project_status
>    else
>      puts "No change in status" #Debugging
>    end
>  end
> end

@status needs to be an association proxy, not the raw ProjectStatus  
object. easiest way out is problably to alias_method the old status=  
method so that you can call it easily from  your status= method.

Fred
>
>
> class ProjectStatus < ActiveRecord::Base
>  has_many :project, :foreign_key => "status_code"
>
>  def > (status); compare_status(status, ">"); end
>  def >= (status); compare_status(status, ">="); end
>  def < (status); compare_status(status, "<"); end
>  def <= (status); compare_status(status, "<="); end
>
>  def self.find_by_code (code)
>    ProjectStatus.find(:first, :conditions => ['code = ?', code])
>  end
>
>  def to_s; self.status; end
>  def to_i; self.id; end
>
>  # Setup some shortcuts i.e. ProjectStatus::SOME_STATUS
>  # will return the status record for that code
>  def self.const_missing(sym)
>    if s = ProjectStatus.find_by_code(sym.to_s)
>      s
>    else
>      super
>    end
>  end
>
> private
>
>  def compare_status (status, op)
>    eval self.order.to_s + op + status.order.to_s
>  end
> end
> -- 
> Posted via http://www.ruby-forum.com/.
>
> >


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to