[LRUG] Updating activerecord associations without saving

Tim Cowlishaw tim at timcowlishaw.co.uk
Thu Sep 23 07:02:00 PDT 2010


On 23 Sep 2010, at 14:58, Anthony Eden wrote:

> Again, how about
> 
> class Person
>   attr_accessor :underpants_i_am_trying
> end
> 
> person.underpants_i_am_trying = boxers
> 
> When you're ready to commit to those underpants:
> 
> person.underpants = person.underpants_i_am_trying
> 
> -Anthony

Yep, was just giving that a go before replying :-)

We've settled on:

class Person

	has_one :underpants
	attr_accessor :trying_underpants

	alias_method :db_underpants, :underpants
	private :db_underpants
	def underpants
		@trying_underpants || db_underpants
	end

`	def commit_to_underpants
		underpants = @trying_underpants
	end
end

That way other parts of our system can still use the underpants method to query what pants the person is wearing at this precise moment, and we get the 'try and commit later' functionality we need without the rest of the system needing to be bothered by it.

Thanks very much for your help!

Tim


More information about the Chat mailing list