I have a nested form (very similar to the one shown in RailsCasts) that I use an update_attributes, which updates the parent object as well as updating and creating new child objects. I want to be able to differentiate between when a child object is created and when a child object is updated and when the parent object is updated. Is there anyway to do so? My immediate goal is to push a broadcast when a new child object is created.
Trying to use an after_create do in the child model doesn't work for my purpose because I'm trying to call an action in the application_controller.rb.
The code is my parent object controller is
if #parent_object.update_attributes(params[:parent_object])
...want the push_broadcast to go here if it's a new record.
If I use debugger, params[:parent_object] returns this
{"overall_sharing"=>"1", "permissions_attributes"=>{"0"=>{"id"=>"66"}, "1"=> {"can_edit"=>"1", "can_copy"=>"1", "_destroy"=>"false", "id"=>"74"}, "1341967403169"=>{"user_id"=>"171", "can_edit"=>"1", "can_copy"=>"1", "_destroy"=>"false", "note_id"=>"423"}}}
for reference, the last bit is the new child object, the first is updating the parent object and the middle is updating the child objects.
I don't know if the correct path is the try to mess with the params or to think of a different route. Any advice would be much appreciated.
Thanks, Welles
If it's a new record it won't have an id. Unlike when editing or updating.
Just try to find it. If you can't then it's new.
if params[:id]
#existing record
else
#new record
end
Rails api has a nice documentation for nested attributes. (Rails guide falls short in this regard, in my opinion). I think it should answer most of your questions.
Related
I know how to get an item of the particular table. Like for user we can have
$userItem = Engine_Api::_()->getItem("user", $userId);
or for a custom table
$customItem = Engine_Api::_()->getItem("custom", $customeId);
I want to know the code or method how can I make my $customItem to work the same way as $userItem works for users table. So that I can get data or manipulate the data of custom table
Thanks for your help. :-)
You can achieve that by creating a model. Check how it's done in /application/modules/User/Model. There is User.php file that declares User_Model_User and methods available for User such as getTitle, get Href etc.
You may use similar approach for your custom item. You will also need to create a file similar to /application/modules/User/Model/DbTable/User.php to declare table for your custom items.
I have a form with a "reset this collection" button. Looks kind of like this:
<button e-click="reset_patients">reset patients</button>
In my controller, I do this:
def reset_patients
puts "destroying"
store.patients.each{|p| p.destroy}
end
What I expect is that the clients displaying the list will show an empty list. What is actually happening is that some but not all of the items are deleted.
How is a "dump the entire collection in the trash can" operation handled on a persistent backed store (i.e.: model :store)? Also, is there a way to make these cascade through related collections?
We don't have .destroy_all yet. Its on my short list, but I'm reworking one thing in the data provider API to make it a bit smarter. For now you can do
store.patients.reverse.each(&:destroy)
(The .reverse is needed since your deleting array objects as you loop)
Let's say you have in your code an object of model 'account.invoice' and inside a method you want to update the partner. I noticed that you have two ways of calling the 'write' method on model 'res.partner'. You could either do :
invoice.partner_id.write({'name': 'Mister Test'})
OR
partner_obj = self.pool.get('res.partner')
partner_obj.write(cr, uid, invoice.partner_id.id, {'name': 'Mister Test'})
I always used the second way because it is the one that is always described in documentations. However, I discovered that the first way is also working and is shorter. Is it ok to do so ?
When object is browse record than I direct write browse record object.write({'field_name': value})
invoice.partner_id.write({'name': 'Mister Test'})
This line give error because partner_id is a many2one field so it's store integer number. So you can’t use this.
For this you must to browse that partner_id and than you may to write on partner object.
And second point, if you want write something in invoice object than you can use this for example invoice.write({'field_name': value}) this will work.
Hope this make a sense.
I need to store a value from the database in a variable, and I need to access this variable from all the actions in my controller.
For example, I have the controller home which has 3 actions:
about
contact
blog
I tried to store the value to a variable in application_controller.rb, but this did not work.
How can I do this?
This is about persistence. To get around the fact website are stateless you persist data by storing it in a cookie. Rails gives you a convenient way of doing this called a session hash.
You can also use the flash hash (This is also a session hash)
At the point where you get the value that you wish to store simply call
session[:some_variable] = some_variable
Then when you want to retrieve that variable just call
some_variable = session[:some_variable]
Replace some_variable with a something that makes sense to you
If you want to do this for all controller actions then a before_filter can be useful
Read here about before_filters http://apidock.com/rails/ActionController/Filters/ClassMethods/before_filter
Do not store large objects or arrays of objects in the session. If you need to keep a reference to an active_record object then store just the ID of the record then you can retrieve that record with a find when you need it
If you want to access to a specific variable in all your controller, just do the following.
Into the application controller :
before_filter :set_my_variables
private
def set_my_variables
#variable = MyModel.find(YOUR_CONDITION)
end
And for instance, in your home controller, you will be able to access to #variable.
Hope this helps.
I am following an article that explains how to use the ICustomAttributeDataHandler class.
I am creating a custom column for the inbox screen, but the problem is that the value I set for my custom attribute is not being reflected on the screen.
As a test I am changing the task name to "whoKnows". But this code is not effecting what is output on the screen:
ICustomAttributeRecordSet.setCustomAttributeValue(i, "taskName", "whoKnows");
(I am able to print debug lines from my custom class when the inbox is viewed, so I know my code is being run.)
Someone on the comments of that article wrote:
the user must call the
"setCustomAttributesInQuery() method
on the dataprovider passing in a
string array of the custom attributes
...what does that meen? Could this be my problem?
thanks.
To be honest, I have already used Webtop, but just as an user. I found a post in the dm developer discussion group that can be useful, though:
For creating a custom column in the
doclist you dont need to go through
this complex procedures. You can use
custom attribute datahandlers for
this.
First in your object list component xml file add your custom column
definition in the "columns" tag. You
can even add static columns instead of
the documentum attributes.
Now create a class which implements the ICustomAttributeDataHandler.
Implement the default the methods getRequiredAttributes and the getData
function.
In getRequiredAttributes add attributes of the object that you are
looking for.
In your getdata method retrieve each row and then based on the
attribute that you see, just set the
value that you want to. 6) Finally
define your class in the app.xml file
There is a section in WDK developement
guide regarding
ICustomAttribuetDataHandlers. Look for
the topic named "Adding custom
attributes to a datagrid".
I'm not sure if this is the final solution, but I hope it helps!
To answer you question about setCustomAttributesInQuery()
every datagrid in WDK is backed by an underlying data provider. You can get this proivder by using the following code.
Datagrid datagrid = (Datagrid)getControl("doclist_grid",com.documentum.web.form.control.databound.Datagrid.class);
DataProvider dp = datagrid.getDataProvider();
Once you've done that, you can call
dp.setCustomAttributesInQuery(myArr);
I'm not actually sure if this is part of the solution to your problem, but you could try this and see where it gets you.
You have to configure the inbox component.
if using classic view, go to inboxlist component and add your custom attribute.
<column>
<attribute>CustomAttributeName</attribute>
<label>Custom Attribute Label</label>
<visible>true</visible>
</column>
Your custom attribute has to be in a custom type that is a sub type of dmi_queue_item, because inboxlist shows only dmi_queue_item objects.
Hope this helps,
Regards,
Tejas.
This may be a non-issue, but based on your code, I can't tell if you're doing this:
ICustomAttributeRecordSet.setCustomAttributeValue(i, "taskName", "whoKnows");
or this:
ICustomAttributeRecordSet rs;
rs.setCustomAttributeValue(i, "taskName", "whoKnows");
You should be calling the setCustomAttributeValue method on the rs object instance, not on the interface.