I've been trying to use aliases to change column names
I have three tables : name , member and payment
I took two different syntax from the internet and I tried them both
first one:
SELECT n.firstname AS [First Name], n.lastname AS [Last Name], m.username AS UserName,
m.birthday AS Birthday, m.email AS Email, m.phone_no AS Phone,
p.MembershipType AS [Membership Type]
FROM member AS m, name AS n, payment AS p
WHERE (m.username = n.username) and (m.username = p.username)
ORDER by n.firstname;
second one:
SELECT n.firstname 'First Name', n.lastname 'Last Name', m.username
'UserName', m.birthday 'Birthday',
m.email 'Email', m.phone_no 'Phone', p.MembershipType 'Membership Type'
FROM member m, name n, payment p
WHERE (m.username = n.username) and (m.username = p.username)
ORDER by n.firstname;
But it display "FROM keyword not found where expected"
what did I do wrong?
I am assuming that you are using Oracle database.
If the alias_name contains spaces, you must enclose the alias_name in quotes
So your query might look like:
SELECT n.firstname "First Name", n.lastname "Last Name", m.username
"UserName", m.birthday "Birthday",
m.email "Email", m.phone_no "Phone", p.MembershipType "Membership Type"
FROM member m, name n, payment p
WHERE (m.username = n.username) and (m.username = p.username)
ORDER by n.firstname;
Related
scope :accessible_by, -> (current_user) { where("loc_primary_email= ? OR loc_backup_email= ? ",current_user.email, current_user.email) }
How could I replace ? with :email so, that I don't need to pass current_user.email twice in query
You can replace it just like that, for the "?":
scope :accessible_by, -> (current_user) { where("loc_primary_email = :email OR loc_backup_email = :email ", email: current_user.email) }
But then the argument for where must contain a hash, where the email key must be present.
You can check the docs for this, using an array of as argument (of course, you can omit the brackets);
Alternatively, you can use named placeholders in the template, and
pass a hash as the second element of the array. The names in the
template are replaced with the corresponding values from the hash.
User.where(["name = :name and email = :email", { name: "Joe", email: "joe#example.com" }])
# SELECT * FROM users WHERE name = 'Joe' AND email = 'joe#example.com';
I'm trying some code to find an attribute with a time value that is less than current time.
If I have current_time = Time.now, how do I find it using where such as:
Outage.where("end_time < current_time") # this doesn't work.
There are many ways to do that via placeholder.
Using ? Placeholder
You can use ? as a placeholder in a query condition.
User.where('users.name = ?', 'John')
With multiple placeholders:
User.where('users.name = ? AND users.last_name = ?', 'John', 'Smith')
Using Named Placeholder
User.where('first_name = :first_name', { :first_name => 'John' })
With multiple placeholder:
values = { :first_name => 'John', :last_name => 'Smith'}
conditions = 'first_name = :first_name AND last_name = :last_name'
User.where(conditions , values)
Noted that order does not matters. The following code would work correctly since we have already named those placeholders.
values = { :last_name => 'Smith', :first_name => 'John'}
conditions = 'first_name = :first_name AND last_name = :last_name'
User.where(conditions , values)
References: Using Named Placeholders in Ruby
You can use placeholder - ? and then pass the value, like this:
Outage.where('outages.end_time < ?', current_time)
Suppose I have the following hash in one of my models named Company.
FIELDS = {
TEAM: {
'num_founders': 'Number of Founders',
'num_employees': 'Number of Employees',
'founders': {
'person_info': {
'full_name': 'Full Name',
'first_name': 'First Name',
'last_name': 'Last Name',
'location': 'Location',
'gender': 'Gender',
'biography': 'Biography',
'num_articles': 'Number of Articles'
}
}
}
}
And I have a action in my application controller which renders this hash as json:
def field_names
module_name = params[:module]
category = params[:category]
case module_name
when 'company'
render json: Company::FIELDS[category.to_sym].to_json
end
end
So now if I open localhost:3000/field_names/company/TEAM I'd get the json. But the problem I'm now facing is I need to get the sub hash of this hash too. Like I want to /field_names/company/TEAM/founders/person_info and get the respective object.
To begin with I'd split the url by slashes to get the keys. Im unable to figure out how would I use those strings to access the hash.
If I define the route like get '/field_names/:query', to: 'application#field_names
And If I hit localhost:3000/field_names/company/TEAM/founders
The action should render Company::FIELDS[:TEAM]["founders"] object which would be
'person_info': {
'full_name': 'Full Name',
'first_name': 'First Name',
'last_name': 'Last Name',
'location': 'Location',
'gender': 'Gender',
'biography': 'Biography',
'num_articles': 'Number of Articles'
}
For this my action should do something like:
def field_names
query = params[:query]
keys = query.split("/")
#keys.first::FIELDS[key2][key3]... .to_json
end
How do I achieve this? Thanks :)
How about something like this:
fields = {
TEAM: {
'num_founders': 'Number of Founders',
'num_employees': 'Number of Employees',
'founders': {
'person_info': {
'full_name': 'Full Name',
'first_name': 'First Name',
'last_name': 'Last Name',
'location': 'Location',
'gender': 'Gender',
'biography': 'Biography',
'num_articles': 'Number of Articles'
}
}
}
}.with_indifferent_access
'TEAM/founders'.split('/').each do |key|
fields = fields[key]
end
puts fields
=> {"person_info"=>{"full_name"=>"Full Name", "first_name"=>"First Name", "last_name"=>"Last Name", "location"=>"Location", "gender"=>"Gender", "biography"=>"Biography", "num_articles"=>"Number of Articles"}}
Just for console, I changed FIELDS to fields (SHOUTING_CASE is usually reserved for constants, fwiw). And, I used with_indifferent_access because your nested hashes use both strings and symbols as keys.
If you want that person_info, then:
fields = {
TEAM: {
'num_founders': 'Number of Founders',
'num_employees': 'Number of Employees',
'founders': {
'person_info': {
'full_name': 'Full Name',
'first_name': 'First Name',
'last_name': 'Last Name',
'location': 'Location',
'gender': 'Gender',
'biography': 'Biography',
'num_articles': 'Number of Articles'
}
}
}
}.with_indifferent_access
'TEAM/founders/person_info'.split('/').each do |key|
fields = fields[key]
end
puts fields
=> {"full_name"=>"Full Name", "first_name"=>"First Name", "last_name"=>"Last Name", "location"=>"Location", "gender"=>"Gender", "biography"=>"Biography", "num_articles"=>"Number of Articles"}
You probably want to put some try in there in case the url is malformed.
Also, you could abstract and simplify that bit where you go:
case module_name
when 'company'
render json: Company::FIELDS[category.to_sym].to_json
end
Assuming you have (in routes.rb):
get '/field_names/:query'
And you hit:
localhost:3000/field_names/company/TEAM/founders
Then your params should include:
{query: 'company/TEAM/founders'}
In which case you do something like:
def field_names
query_split = params[:query].split
module_name = query_split.shift.camelize
fields = "#{module_name}::FIELDS".constantize.clone.with_indifferent_access
query_split.each{|key| fields = fields[key]}
render json: fields
end
You don't have to put to_json at the end of your hash, btw.
It's a routing problem in your project. Actually, you should have a route to retrieve these fieldnames, like:
get 'field_names' => 'controller#action'
then, you can pass any parameters on your URL.
Eg.
/field_names?company=TEAM&attribute=founders
Than, those attributes would be available in your controller as
params[:company]
params[:attribute]
and so you can render only the attributes you want
I now, that question could be non good for so, but need help:
In my rails haml view i have such code:
%table.table.table-striped
= form_tag :admin_other_price_upload do
%tr
- #csv.first.length.times do |n|
%th
= n + 1
%br/
=select_tag "s"+(n+1).to_s, options_for_select([["Брэнд", "Brand"], ["Артикул","Article"], ["Наименование","Descr"], ["Цена","Price"], ["Количество","Quantity"], ["Дополнительно","Other"], ["Поле кроссов","Cross"]]), :prompt => 'Все', :id => "select-value"
*********************************
so as you can see i'm setting to all select's name like s(n+1) and value one from list. But how can i get them both in controller method? I need it becouse i have dynamic table. I will explain it on example:
So i have table with select's
name = s1 (value = Brand) | name = s2 (value = Price)
so i need in controller to get not only that s1 => Brand, but also get this 1 from s1
So if param look's like
[
s1 => {Brand}
]
I need to get for my calculation s1 not value, but s1 as string (simple i need to find in params, which value has Brand and select it as a value)
So for Brand i need to select s1, and set as s1 value s1, how could i do it?
I may have understood you, but not sure.
# let's say your params hash is like:
params = { :action => 'show', :controller => 'articles',
:s1 => 'Article', :s2 => 'Brand', :s3 => 'Price', ... }
brand_param = params.select{ |key, value| value == 'Brand' }
# => { :s2 => 'Brand' }
which_s_is_brand = brand_param.keys.first.to_s
# => 's2'
i have firstname,lastname and id of a list of people in database.
Now i want to make an array like
[{
value: <corresponding id>,
label: "<corresponding person name>"
},
{
value: <corresponding id>,
label: "<corresponding person name>"
},
....
]
users = User.all
user_hash_array = users.collect{|user| {:value => user.id, :label => user.firstname}}
This will work like following
id firstname lastname
1 Salil Gaikwad
2 Nidhin Bose
This will gives you following
user_hash_array = [{:value=>1, :label=>"Salil"}, {:value=>2, :label=>"Nidhin"}]