simpledb: overwrite an item and replace all attributes - amazon-simpledb

How can I overwrite an item and replace all its attributes.
For eg., I have an item A, with following attributes
ItemA
AttrA = ValueA
AttrB = ValueB
now I want to update ItemA with the following attributes
ItemA(Updated)
AttrB = ValueB1
AttrC = ValueC
I am using the Java SDK. I don't want to delete the item and then create it. Currently, I am using BatchPutAttributesRequest to re-create the item but surprisingly, it doesn't delete the attributes that are not present in the updated item.
So I end up with the Item
ItemA
AttrA = ValueA
AttrB = ValueB1
AttrC = ValueC

BatchPutAttributesRequest request does not delete existing Attribute that you had not included in your request. You need to send an another request DeleteAttributesRequest to Amazon SimpleDB by specifying list of Attributes that you wish to delete. BatchPutAttributesRequest can only add new Attribute or can update existing value of an attribute.

Related

How to select columns from active record?

Using Devise to set the current user and current company,
I have an active record of User which contains certain fields that should not be exposed in some cases like password etc.
#<User email:*** password:***>
How can we select only selected attributes and keep them as active record object only.
output should be
#<User email:****>
current_user is getting created from devise helper.
In Active Record, you can use the select method to choose the attributes you want to retrieve from the database. Here's an example:
current_user.select(:email)
This will return a new Active Record object with only the email attribute populated, while keeping the other attributes as nil.
Note that select returns a new instance of the Active Record object and does not modify the original object. To persist the changes, you need to assign the result to a new variable or update the original object:
selected_user = current_user.select(:email)
Alternatively, you can use the attributes method to retrieve a hash of attribute names and values, and then select only the desired attributes:
attributes = current_user.attributes.slice("email")
selected_user = User.new(attributes)
This will give you a new instance of the User model with only the selected attributes.

One form input to set two database values

I have a form which when submitted creates a new record in a database. I have two values in the database, value_1 & value_2. In the form there is an input field for value_1 which is a dropdown value of yes and no. When the form is submitted I wish to have value_1 and value_2 set to the value selected in the input field for value_1. So if dropdown for value_1 is set to yes then value_2 is also set to yes.
I am currently using the following but believe there must be a more elegant solution:
params[:person][:free] = params[:person][:trial]
#person = Person.new(params[:person])
#person.update_attribute(:free, params[:person][:free])
First you should think about what you really want. What is your logic.
Do you want value_2 always equal to value_1? I hope not, because in this case u absolutely no need a second column in your database.
Do you want value_2 equal to value_1 only the creation of a new record but allow it to be edited after that? In this case you should put the logic in your model with a callback :
In your model add :
before_create :set_value_2
private
def set_value_2
self.value_2 = value_1
end
The private statement is because you don't want anybody to access the method outside the model itself.

Rails create multiple instances if title is null?

Right now I have a create action in one of my controllers which will update a submission if it finds one in the system with the same title as the one being created. However, if the title is null I'd like it to create new instance anyway.
The piece of code looks like this:
#submissions = Submission.where(title: ajax_title)
So if it finds a instance of the Submission model with the same title as the one being created, it'll just update the current one instead of creating a new instance. However, I'd like to go ahead and create new instances if the user didn't input a title, regardless of if there are any other Submissions in the system with a null title.
How do I do this?
Make a judgement about whether the query result is blank or not:
#submissions = Submission.where(title: ajax_title)
#submission = Submission.create if #submissions.blank?
You'll want to just add another line that only creates a new submission if the title is blank:
#submissions = Submission.where(title: ajax_title)
#new_submission = Submission.create if ajax_title.blank?

Add empty value to a DropDownList in ASP.net MVC

I'm building a data entry interface and have successfully bound the columns that have reference tables for their data using DropDownList so the user selects from the pre-configured values.
My problem now is that I don't want the first value to be selected by default, I need to force the user to select a value from the list to avoid errors where they didn't pick that field and by default a value was assigned.
Is there a more elegant way of doing this than to add code to include an empty value at the top of the list after I get it from the database and before i pass it to the SelectList constructor in my controller class?
The Html helper function takes a 'first empty value' parameter as the third argument.
<%=Html.DropDownList("name",dataSource,"-please select item-")%>
You can also use this way:
dropdownlist.DataTextField = ds.Tables[0].Columns[0].Caption;
dropdownlist.DataValueField = ds.Tables[0].Columns[1].Caption;
dropdownlist.DataSource = ds;
dropdownlist.DataBind();
dropdownlist.Items.Insert(0, new ListItem("Select ...", string.Empty));

ASP.NET MVC using UpdateModel to post child records

Continuing from this question, I've got a form with all the Vehicles listed for a Person, the vehicle fields are editable, and they are now successfully posting back to my Save action.
Now I'd like to use UpdateModel to save the data, but I'm not sure how to construct it. Here's my Save action right now:
<ActionName("Edit"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As Person, ByVal vehicles() As Vehicle) As ActionResult
Dim original = From v In MyDataContext.Vehicles Where v.person_id = Person.person_id
For Each item In original
For i = 0 To vehicles.Count - 1
If vehicles(i).vehicle_id = item.vehicle_id Then
UpdateModel(item, New String() {"license_nbr", "color"})
Exit For
End If
Next
Next
MyDataContext.SubmitChanges()
Return RedirectToAction("Index", "Home")
End Function
When I run this, it doesn't save anything, and UpdateModel doesn't throw any errors. I'm assuming I have to give it a little more direction to get the magic to work, because UpdateModel doesn't know which item in the vehicles array to use for each update.
Do I need to specify a ValueProviderResult as the third parameter to UpdateModel? If so, how do I create one out of vehicles(i)? Am I completely off base in how I have this set up?
Why use UpdateModel -- which just updates the properties from form fields -- when you already have the form fields processed into model data? Can't you just assign the values from vehicle to item directly?
For Each item In original
For i = 0 To vehicles.Count - 1
If vehicles(i).vehicle_id = item.vehicle_id Then
item.license_nbr = vehicles(i).license_nbr
item.color = vehicles(i).color
Exit For
End If
Next
Next

Resources