last day (14 days later) » 

5:58 AM
1
A: Saving multiple updates and creates as one transaction - ruby rails

Pamio SolankyAlthough, its a bit difficult to understand what exactly your requirement is but as far I understand this is what you want, In Rails 3 - PlayerType.find_or_initialize_by(id: p_type_params[:player_id]) And in rails 4 - PlayerType.where(id: p_type_params[:player_id]).first_or_initialize The ab...

 
I dont get it exactly, my concern is to make the updates and the create that is happening to be atomic. In the update, i supply with an id so that the corresponding column in the parameter is updated and in the create i dont supply because id is auto generated. And when I create I need some more control on creation so I need to do it as a if,else.
 
@player_type = PlayerType.find_or_initialize_by(id: 'someid or nil'), this statement will always return @player_type, after which you can also use the object to updates the attributes. If the id that your passing is nil then it will create a new object and which you can save after assigning any other values.
 
Okay, I kind of understand, I may have missed an information trying to simplify my problem. While I create a player, I create this way squad.player_type.new(p_type_params), so that squad_id is initilized as its a foriegn key to PlayerType. Can I still get it to work the way you are suggesting?
 
Yes you can. You may as well think of having find_or_initialize_by on multiple columns which is what you want.
 
Any reason you would know why it would say undefined method find_or_initialize_by'`
 
5:58 AM
Depends. which rails version ? Are you invoking this on on a class ? or object ? or tell me how exactly your code looks like only then its possible for people to point out where your going wrong
 
PlayerType is a model class player_type.rb, Let me see if i can elaborate the question. Also I am using rails 3
I figured the issue, but it says cant mass assign id, which should not be the case right because ideally I only want it to find by id or initialize id and not assign it.
 
When you initialise a new object, it does assign values that it received but its just that the object isn't saved yet. Can you show me where you getting the issue ?
 
So when I did this PlayerType.find_or_initialize_by_id(id: p_type_params[:id]) it gave me mass assignment error
 
Ok, can you allow the id params in your model ?
 
no, unfortunately that attribute should not be accessible
I thought find_or_initialize_by_id this would find by id or autoinitialize if id is nil, should it be trying to assign it?
 
6:02 AM
when the id is nil, it initializes object with id as nil
basically, your explicitly asking active record to initialise an object with whatever is passed .. so its assignment basically
 
Oh so that is not the intention, because if id is not passed it is autoincremented and assigned value
it should not take nil
 
basically, when id is nil or anything thats not valid, for example "" ie. an empty string, it will just assign the id as nil and initialise the object
this is how it works
as far as I know
 
Oh okay, is there any alternative to get the update and creation of a row as a transaction to make it atomic
?
 
You will have to write your own :) But this is what generally I and I'm sure most people use.
 
Oh i see, so I will have to make the id accessible for this to work right
?
 
6:08 AM
yes, you will have to. Also this generally also is the case when you want to update a record. From the form you always pass an id field which is again accessible in the model. So its the right approach. But if you think of a better approach do let me know
 
Sure great thanks for your time and inputs, I will try it soon. !!
 
Sure
If the answer was helpful then do not forget to accept it :)
 
definitely !!
One more quick question if you are there, I do player_type = PlayerType.find_or_initialize_by(id: 'someid or nil') but since its an iteration I need to store the return type in an array and do a bulk save ?
 

last day (14 days later) »