How to alias a table name? - ruby-on-rails

Since upgrading to Rails 5 I have a query that is not working. It is unable to get results from a table by its alias. The error is that "from" takes zero arguments now. The version of arel is 9.0, the version of Rails is 5.2.4.3
offers = Offer.arel_table
o2 = offers.alias("o2")
seen_offers = offers.from("offers as o2").project(o2[:merchant_id], o2[:display_name])
this is the error:
ArgumentError: wrong number of arguments (given 1, expected 0)

Arel::Table#from takes 0 arguments because it generates a SelectManager:
SelectManager.new(self)
What you are looking for is SelectManager#from and the issue is easily correctable (just switch the project and the from)
offers.project(o2[:merchant_id], o2[:display_name]).from(o2)
project will return a SelectManager and then from will simply change the source

Related

Doorkeeper 5.4.0 ArgumentError

I recently updated my app, from doorkeeper 5.3.3 to 5.4.0 but changes to Doorkeeper::AccessToken.find_or_create_for were made apparently, but I can't find anything in the CHANGELOG
so
#access_token = Doorkeeper::AccessToken.find_or_create_for(
oauth_client,
model.user.id,
oauth_scopes,
oauth_expiry_time,
oauth_server.refresh_token_enabled?
)
returns now an ArgumentError
ArgumentError (wrong number of arguments (given 5, expected 0; required keywords: application, resource_owner, scopes)):
Has this also happened to anybody else?
Doorkeeper::AccessToken.find_or_create_for seems to expects keyword arguments instead of a simple list of arguments nowadays.
#access_token = Doorkeeper::AccessToken.find_or_create_for(
application: oauth_client,
resource_owner: model.user.id,
scopes: oauth_scopes,
expires_in: oauth_expiry_time,
use_refresh_token: oauth_server.refresh_token_enabled?
)

Ruby gem origami sign signature failed

I'm trying to make the PDF file with signature with origami gem, follow this example https://github.com/gdelugre/origami/blob/master/examples/signature/signature.rb
Now i just run this signature.rb and got error
[error] Breaking on: ">>\nendobj\n..." at offset 0x1f6f3
[error] Last exception: [Origami::InvalidObjectError] Failed to parse object (no:43,gen:0) -> [ArgumentError] wrong number of arguments (given 1, expected 0; required keyword: year)
I have no idea to move forward :'<
I also found the sign method of gem at https://github.com/gdelugre/origami/blob/master/lib/origami/signature.rb, and have take a look, I can't find any specific things to do :'<
Might this example is outdate?
The error message is known see https://github.com/gdelugre/origami/issues/80
A fix should be available https://github.com/gdelugre/origami/pull/74/commits
But has not been added so use newer file from the fork https://github.com/pocke/origami/tree/fix-ruby-2.7-kwargs-warnings
Specifically you need this updated file and may need to follow any other suggestions from above.
https://github.com/pocke/origami/raw/fix-ruby-2.7-kwargs-warnings/lib/origami/filters/predictors.rb
However it always worth looking for a fork with many more recent improvements such as
https://github.com/joelsondrew/origami

Sass Syntax Error: wrong number of arguments (1 for 3) for `rgb'

I use Ruby On Rails 5 and gem sass-rails 5.0
For some reason I get an error message:
wrong number of arguments (1 for 3) for `rgb'
in line number 2 of file app/assets/stylesheets/main.css.scss
$main-color-raw: rgb(0,80,170);
$main-color: rgb($main-color-raw);
$main-color-bright: rgba($main-color-raw, 0.5);
I tried with a hex color value instead of rgb():
$main-color-raw: #0050aa;
But get the same error message.
It looks like the sass-variable $main-color-raw is not evaluated correctly and maybe is the rails-sass gem configuration wrong.
So I added in my config/application.rb
config.sass.preferred_syntax = :scss
config.sass.line_comments = false
config.sass.cache = false
from the rails-sass github documentation page.
But still the same error.
How can I resolve it?
$main-color-raw: rgb(0,80,170);
so in $main-color, you've got:
$main-color: rgb(rgb(0,80,170));
so you are passing only 1 argument to rgb.
You should declare main-color-raw as:
$main-color-raw: 0, 80, 170;

postgres - operator does not exist: double precision ~~ unknown

ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator
does not exist: double precision ~~ unknown
2016-04-10T23:45:59.506005+00:00 app[web.1]: LINE 1: ... =
"trackers"."category_id" WHERE (categories.tag LIKE '1.%'...
this is the error i get when i try to run this line of code here
Tracker.group(:category_id).joins(:category).where('categories.tag LIKE ? AND user_id = ?', "#{tag.to_i}.%", current_user.id)
tag is of type float, and i typecast it to an integer in order to check for tags 1.1, 1.2, 1.3 etc
so in the example above I type cast tag with value 1.0 to be 1, so i can search for tags that are like 1.1, 1.2 etc
I am using postgres on heroku that gives this error. locally i use sqlite3 and it works just fine.
how can i overcome this?
Since you're in rails, sort out the dynamic-ness in rails first then send that to the ORM. The syntax you provided already accepts any parameters (eg: WHERE tag between ? and ?), so before you request the data from the ORM, sort out in rails the high and lows. The query is already setup for something to be dynamic.

Postgresql WHERE clause adds '' around?

This morning I switched to postgresql and now my where selects dont work anymore.
What I am trying to do is super simple:
shirt_ids = "1,5,6" # String generated by javascript
Shirt.where("id in (?)", shirt_ids)
This gives me :
PG::Error: ERROR: invalid input syntax for integer: "1,5,6"
LINE 1: SELECT "shirts".* FROM "shirts" WHERE (id in ('1,5,6'))
This works though:
Shirt.where("id in (#{shirt_ids})")
But as everybody knows is unsafe.
Im using:
pg (0.13.2 x86-mingw32)
rails (3.2.2)
Postgresql database is the newest version, I installed it this morning.
Thank you for your help.
^
I believe Postgres is expecting an array, rather than a string for the IN function. If you convert your string to an array, it should work:
shirt_ids = "1,5,6"
Shirt.where('id in (?)', shirt_ids.split(','))
Also, you can do this, which looks a bit cleaner:
Shirt.where(:id => shirt_ids.split(','))

Resources