ruby on rails - How to store an id in a model class -


It should be an easy thing to make sure I really know how the title is, though. I have a collection of modules which can be added to my CMS (Page, Link, Form ...), all are connected to a table through a polymorphic association which manages common things like ancestors And what are they capable of etc. .. Because I'm creating a 'page' for example, I automatically handle the creation of this second table record. I need to pass it to parent_id To do this, I have created a proxy through the page model:

Prefix parent_id = (parent_id) @parent_id = parent_id.blank? ? Nil: parent_id.to_i component_instance.parent_id = @parent_id end def parent_id @parent_id || = Component_instance.parent_id end

component_inthe is a multi-union.

You can see when parents are set to the page, then I am checking some magic that it is empty and if so, set the @parent_id value to zero Otherwise, I am allocating it to the integer, then I am allocating it as a component, it will be applicable when the whole page / component_computer will be saved in the database.

I had to clear ? Zero: parent_id.to_i because the Form Selector Assistant expects an integer and does not work with the string value for the id.

The question is, am I doing this correctly, or is there a better way to turn a string id to either zero or an integer if it exists or not?

I ended up setting up the actual model of these 'virtual' attributes where they were 'real 'Were:

  rep: parent_id,: parent_id = ,: to = & gt; : Component_instance ,: allow_nil = & gt; Truth  

prevents the need to type first attribute.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -