no such column: COLIN (SQLite3::SQLException) - ruby

My code is giving me the no such column error on my update function. It's returning the right output but still giving an error. I'm just want the update function to modify the first name when ran. Any ideas why?
class User
def self.create(user_info)
DBConnection.execute(<<-SQL)
INSERT INTO
users (firstname, lastname, age, password, email)
VALUES
('#{user_info[:firstname]}', '#{user_info[:lastname]}',
'#{user_info[:age]}', '#{user_info[:password]}', '#{user_info[:email]}')
SQL
DBConnection.last_insert_row_id
end
def self.find(user_id)
DBConnection.execute(<<-SQL, user_id)
SELECT* FROM
USERS
WHERE
id = ?
SQL
.first
end
def self.update(user_id, attribute, value)
DBConnection.execute(<<-SQL, user_id, attribute, value)
UPDATE
users
SET
#{attribute} = #{value}
WHERE
id = #{user_id}
SQL
end
end
user1 = User.create(firstname: "Colin", lastname: "Doe", age: "25", password: "password", email: "bla")
user2 = User.create(firstname: "Jane", lastname: "Doe", age: "25", password: "password", email: "bla")
user3 = User.create(firstname: "Ted", lastname: "Doe", age: "25", password: "password", email: "bla")
User.update(1, :firstname, 'COLIN')
print User.find(1)``
```

Is it just this typo?
def self.create(user_info)
DBConnection.execute(<<-SQL)
Should be:
def self.create(user_info)
DBConnection.execute(<<-SQL,

Related

Elastic Search Nest Fuzzy per Field

I am struggling with Elasticsearch using NEST for C#.
Let's assume an index of UserAccounts which looks like
[{
AccountId: 1,
Name: "Test Account",
Email: "test#test.com",
Phone: "01234/5678",
Street: "test Street 1",
Zip: "12345"
},
{
AccountId: 2,
Name: "Test Akkount",
Email: "test#gmail.com",
Phone: "0987/6543"
Street: "test Street 1",
Zip: "54321"
},
{
AccountId: 3,
Name: "Bla Bla",
Email: "qwer#yahoo.com",
Phone: null,
Street: "bla Street 3",
Zip: "45678
},
{
AccountId: 4,
Name: "Foo",
Email: "asdf#msn.com",
Phone: null,
Street: "ghjk Street 9",
Zip: "65487,
}]
now I want get all accounts similar to my query.
string name = "aggount";
string email = "test#gmail.com";
string phone = "0987/6543";
string street = "test Str 1"
string zip = "54321"
But each field has its own criteria.
Field "name" should match over fuzzy logic.
Field "email" should match to 100% but not when its null or empty.
Field "phone" should match to 100% but not when its null or empty.
Field "street" should only match with fuzzy, when "zip" matches to 100%.
I want a list of account with possibilities. If name matches but email not, than there should be a result because of name. Do elastic trim always the provided values?
If it is possible to get a score per field. But this is a nice to have.
My code do not work because when I provide a email and the email is not matching, elastic skip the match over name.
var response = elasticClient.Search<Accounts>(search => search
.Index(INDEX_NAME_ACCOUNT)
.Query(q => q.Bool(b =>
{
if (name != null)
{
b = b.Should(s => s.Match(m => m.Query(name).Field(f => f.Name).Boost(1.5).Fuzziness(Fuzziness.EditDistance(3))));
}
if (street != null && zipCode != null)
{
b = b.Should(s =>
s.Match(m => m.Query(street).Field(f => f.MainAddress.Street).Boost(0.5).Fuzziness(Fuzziness.EditDistance(3))) &&
s.Match(m => m.Query(zipCode).Field(f => f.MainAddress.Zip).Boost(0.7).Fuzziness(Fuzziness.EditDistance(0)))
);
}
if (string.IsNullOrEmpty(name1) && string.IsNullOrEmpty(street))
{
b = b.Should(s => s.MatchNone());
}
b = b.MustNot(s => s.Match(m => m.Query(null).Field(f => f.DeletedTimestamp)));
return b;
}))
.Explain()
.Human()
);
Thank you in advance

Laravel Eloquent to get relational table data by one column as a key and another as value

I have two tables users and user_profiles with the following schema
Users Table
id
username
email
password
role
is_root
created_at
updated_at
User Profiles Table
id
user_id (FK -> users -> id)
key
value
User Modal
public function profileData()
{
return $this->hasMany(UserProfile::class);
}
UserProfile Modal
public function user()
{
return $this->belongsTo(User::class);
}
So if I use $user->profileData it is giving me this result, which is correct.
=> Illuminate\Database\Eloquent\Collection {#3244
all: [
App\UserProfile {#3251
id: 3,
user_id: 10,
key: "country",
value: "India",
},
App\UserProfile {#3242
id: 1,
user_id: 10,
key: "first_name",
value: "John",
},
App\UserProfile {#3252
id: 2,
user_id: 10,
key: "last_name",
value: "Doe",
},
],
}
Question
However, I wan to fetch the value field's value/data from the user_profiles table by providing the key fields value by
user id. For instance, if I pass the country it should return
India or first_name then John in the above example. How to
achieve that?
U need to use where(), select() and value() statement for needed value
UserProfile::where('user_id', $id)->where('key', $key)->select(['key','value'])->value('value')

Create a mutation which takes multiple values of an ENUM

I've created a model that has a field which takes multiple values of an ENUM. How can I write a mutation which will allow me to add multiple values at once?
For example, I have a sign up mutation:
export const signUp = gql`
mutation signUp(
$email: String!,
$name: String!,
$password: String!,
$attended: [USER_ATTENDED!]!,
$role: [USER_ROLE!]!,
) {
createUser(
authProvider: {
email: {
email: $email,
password: $password,
},
},
name: $name,
attended: $attended,
role: $role,
) {
id,
createdAt,
name,
email,
}
}
`;
I would expect $attended and $role to accept multiple values.
You can pass multiple values by using brackets: []. In your case, the variables in GraphiQL need to like like this:
{
"email": "name#email.com",
"name": "Name",
"password": "secret",
"attended": ["A", "B"],
"role": ["ROLE_A", "ROLE_B"]
}
In Apollo, this should work:
const variables = {
email: "name#email.com",
name": "Name",
password: "secret",
attended: ["A", "B"],
role: ["ROLE_A", "ROLE_B"]
}
More information about GraphQL variables is available here and here.

Ruby object mass assignment

Is there any better way to do the below code?
user.name = "abc"
user.email = "abc#test.com"
user.mobile = "12312312"
Something like this will do:
user.prepare do |u|
u.name = "abc"
u.email = "abc#test.com"
u.mobile = "12312312"
end
tap let's you do exactly that:
user.tap do |u|
u.name = "abc"
u.email = "abc#test.com"
u.mobile = "12312312"
end
Alternative option when your attributes come in the form of a hash:
attrs = {
name: "abc",
email: "abc#test.com",
mobile: "12312312"
}
attrs.each { |key, value| user.send("#{key}=", value) }
You could do the following as well:
user.instance_eval do
#name = "abc"
#email = "abc#test.com"
#mobile = "12312312"
end
You can access the instance variables of user inside the block given to instance_eval
You could use the below code if you wish to invoke the accessor methods instead of directly manipulating the instance variables.
user.instance_eval do
self.name = "xyz"
self.email = "abc#test.com"
self.mobile = "12312312"
end
or
user.instance_eval do |o|
o.name = "xyz"
o.email = "abc#test.com"
o.mobile = "12312312"
end
With ActiveRecord objects you can use .assign_attributes or the update
methods:
user.assign_attributes( name: "abc", email: "abc#test.com", mobile: "12312312")
# attributes= is a shorter alias for assign_attributes
user.attributes = { name: "abc", email: "abc#test.com", mobile: "12312312" }
# this will update the record in the database
user.update( name: "abc", email: "abc#test.com", mobile: "12312312" )
# or with a block
user.update( name: "abc", mobile: "12312312" ) do |u|
u.email = "#{u.name}#test.com"
end
.update accepts a block, while assign_attributes does not. If you are simply assigning a hash of literal values - such as those passed by a user in the params then there is no need to use a block.
If you have a plain old ruby object which you want to spice up with mass assignment you can do:
class User
attr_accessor :name, :email, :mobile
def initialize(params = {}, &block)
self.mass_assign(params) if params
yield self if block_given?
end
def assign_attributes(params = {}, &block)
self.mass_assign(params) if params
yield self if block_given?
end
def attributes=(params)
assign_attributes(params)
end
private
def mass_assign(attrs)
attrs.each do |key, value|
self.public_send("#{key}=", value)
end
end
end
This will let you do:
u = User.new(name: "abc", email: "abc#test.com", mobile: "12312312")
u.attributes = { email: "abc#example.com", name: "joe" }
u.assign_attributes(name: 'bob') do |u|
u.email = "#{u.name}#example.com"
end
# etc.
Assuming that 'user' is a class that you control, then you can just define a method to do what you want. For example:
def set_all(hash)
#name, #email, #mobile = hash[:name], hash[:email], hash[:mobile]
end
And then in the rest of your code:
user.set_all(name: "abc", email: "abc#test.com", mobile: "12312312")
If 'user' is an instance of, say, an ActiveRecord model, then I'm a little shaky on the details of how you would get this to work. But the principal still applies: DRY up code by moving the responsibility for the complexity to the receiver.

How to add pair to Users Ruby on Rails Class and sort by it?

Controller:
#users = User.all
View:
<%= #users %>
Result [#<User id: 1, email: "a#a.a", location: "London">, #<User id: 2, email: "b#b.b", location: "Paris">]
How I can add to User class one more pair without adding it to model?
something like:
Controller:
#users.each do |user| <br>
#type = Types.find(:last, :conditions => ["user_id_addressedto = #{user.id}"])
user << #type
end
#users.sort_by(:type)
at the end I want to see result as:
View:
<%= #users %>
Result: [#<User id: 2, email: "b#b.b", location: "Paris", type: "5">, #<User id: 1, email: "a#a.a", location: "London", type: "2">]
class User
# ...
def type
return #type if #type
#type = Types.find(:last, :conditions => ["user_id_addressedto = #{user.id}"])
end
end
This way, it's in the model, but not in the database proper - and it's cached, so it's only queried the first time you need it. That is, only if you need it infrequently enough.
Note: if you are pulling 25 users and reading their type, it's 25 extra SQL calls. Maybe you'd rather make an association, or a scope column.

Resources