Monday, August 20, 2018

How to update field in table Laravel or create?

I try to update field rate in table by primary key user_id.

So, I need to increment of decrement rate field. If row is not exist I need to create it.

I tried:

$this->point = -1; // Or $this->point = 1;
Rating::updateOrCreate(["user_id", $id], ["rate" => $this->point]);

But it does not work:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `rating` where (`0` = user_id and `1` = 5) limit 1)

Solved

This is because your passing the column and the value as two separate values.

Change:

["user_id", $id]

to:

["user_id" => $id]

Hope this helps!


Laravel (eloquent) provides a fluent way to update related models.

Assuming the following model structure:

Rating.php

public function user()
{
    return $this->belongsTo('App\User');
}

User.php

public function ratings()
{
    return $this->hasMany('App\Rating');
}

You can easily update the relation via associate()

$user = User::findOrFail($userId);
$rating = Rating::findOrFail($ratingId);  

$rating->user()->associate($user)->save();

Read more about Relationships in Eloquent


Sunday, August 19, 2018

Deduplicate: Different File Contents Found Error for SBT and Scala

I am trying to build a JAR file through SBT, with my driver script being in Scala. However, when I run 'sbt assembly' I receive a large slew of:

deduplicate: different file contents found in the following:

The entire list of directories displaying that message is too large to post here, so I attached a screenshot which you can view below. My build file contains the following:

libraryDependencies ++= Seq("org.apache.spark" %% "spark-core" % "2.0.0" % "provided", "org.apache.spark" %% "spark-mllib" % "2.0.0")

I attempted the solution offered by Onilton Maciel to this question, but to no avail. I am also confused as to how to implement the solution offered by Martin Senne, as the setup instructions on the 'Setup' section for the Assembly plugin are not clear on how to implement his code snippet. Any help would be appreciated. Thanks![enter image description here

Solved

as the setup instructions on the 'Setup' section for the Assembly plugin are not clear on how to implement his code snippet.

It means you should add merge strategy in build.sbt or in root directory sbt files.

There is an example from sbt-assembly official document and update it with your conflicts:

import sbtassembly.MergeStrategy
assemblyMergeStrategy in assembly := {
  case PathList("org", "apache", "hadoop", "yarn", "factories", "package-info.class")         => MergeStrategy.discard
  case PathList("org", "apache", "hadoop", "yarn", "provider", "package-info.class")         => MergeStrategy.discard
  case PathList("org", "apache", "hadoop", "util", "provider", "package-info.class")         => MergeStrategy.discard
  case PathList("org", "apache", "spark", "unused", "UnusedStubClass.class")         => MergeStrategy.first
}

Hopeful it's helpful for you.


Saturday, August 18, 2018

Jasmine real AJAX request test

I need to make a test with a real ajax request with jasmine,

So far I have this:

function login(){
    $.ajax({
        method: 'GET',
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });
}

I need to get the login success response, but for that, jasmine needs to wait for the request to end before iteration trough the 'its'. How can I do that?

Solved

You need to use "done()" and then call it once your async request has been completed. It is a part of the jasmine testing suite. So something like this.

function(done){
    $.ajax({
        method: 'GET',
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });
    done();
}

Shouldn't it be more like

function(done){
 $.ajax({
    method: 'GET',
    url: url,
    dataType: "json",
    contentType: "application/json; charset=utf-8"
 })
 .then(done);
}

or replace done with a method that actually checks

.then(function(response){
   //do your asserts here
   done();
});