Ruby / Datamapper create or update issue - immutable error -


I have a user class, which can optionally be a billing address when I post a payment form Assuming the user has indicated that they want to save their billing address details, I want to create a new address record or update the original.

I have tried to do a lot of things but the closest I can get is the work code ...

  Datamapar in Class User :: Resource Property: ID, Serial Property: Provider, String, Length = & gt; 100 Property: Identifier, String,: Length = & gt; 100 Property: Username, String ,: Length = & gt; 100 Property: remember_billing, 1 near Boolean, Billing_Address and Class Billing Address includes DataMaper :: resource property: first, string, length = & gt; 20 Property: Nickname, String,: Length = & gt; 20 Property: Address1, String, Length = & gt; 50 Property: Address 2, String ,: Length = & gt; 50 Property: Townicity, String,: Length = & gt; 40 Property: State, String ,: Length = & gt; 2 Property: Postcode, String,: Length = & gt; 20 Property: Country, String,: Length = & gt; 2 Property: deleted_at, paranoidetime is_to: user,: key = & gt; True End Post "/ Pay" @ post = params [: post] @addr = params [: addr] If @addr == blue @addr = hash New end user = user.first (: identifier = & gt; session ["vya.user"]) user.remember_billing = !! @ Post ["remember"] If user.remember_billing user.billing_address = billingadder.first_core ({: user = & gt; user}, @addr) end user.save ...   

Works fine when there is no record. But if there is already a record, then it holds the original value.

I have seen a similar post but if I change the code

  user.billing_address = billingadder.first_core (user: user = & gt; user) .update ( @addr)   

I get an error

  DataMapper :: ImmutableError can not be modified on / of fixed resource   

You are adding a lot of things together, There How about:

  billing = billingadder.first_or_new (: user = & gt; user, @addr) # Do not update, send Hash as second parameter billing. ? Billing.update (@addr): billing.save "Billing has not been saved for any reason: # {billing.errors.inspect}" as long as billing & amp; Amp; Billing.saved? User.billing_address = Billing user.save    

Comments