Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting virtual model attribute from controller create action in Rails 4 - attribute always nil
    primarykey
    data
    text
    <p>Some background: I have a Rails 4 model with a decimal column <code>d</code>. When being saved or viewed in the app, the value may need to be converted to or from an integer value, respectively, based on the current user's preference.</p> <p>It seems most appropriate that the calculations should go in the model, and should have no knowledge of users.</p> <p>After doing some searching, it seems to me the best way to achieve this is via 2 virtual attributes on the model: one for the current user's preferred format and one for the value, with any calculation applied.</p> <p>So I have come up with something like this:</p> <pre><code>attr_accessor :format, :value_converted def value_converted if format.nil? format = 'A' end if format == 'A' Converter.new(d).to_i if d else d if d end end def value_converted=(value) if format.nil? format = 'A' end if format == 'A' self.d = Converter.new(value.to_i).to_f else self.d = value end </code></pre> <p>Forms refer to the <code>value_converted</code> rather than the <code>d</code>, and this works fine for editing, updating and viewing. The problem is the <code>#create</code> action.</p> <p>I believe the issue is happening because the setter is accessing <code>format</code>, and I can't seem to figure out a way to set <code>format</code> from <code>#create</code> before the setter is called, in other words when <code>new</code> creates the object.</p> <p>I thought perhaps I could just pass the <code>format</code> along with the other parameters (as in <a href="https://stackoverflow.com/questions/16530532/rails-4-insert-attribute-into-params">this answer</a>, so I tried merging it in, both within and after the standard strong parameters fare, but with no luck:</p> <p>Both</p> <pre><code>@entry = current_user.entries.new(entry_params.merge(format: current_user.format)) </code></pre> <p>and</p> <pre><code>params.require(:entry).permit(:value_converted, :other_param, ...).merge(format: current_user.format) </code></pre> <p>do not raise any errors, but are apparently ignored. However simply passing <code>format</code> to <code>new</code> in the console works fine.</p> <p>So my question: Does anyone have a solution to this problem? Or perhaps a better/more appropriate way of going about it? It seems like it should be something simple.</p> <p>Thanks for any help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload