I'm looking forward to change the develeop and prodoction databses in order to be able to migrate to heroku.
I fallowed this link step by step everything went ok, I restarted the server rails s while my postgresql was started and I got this error
ActiveRecord::StatementInvalid in Tasks#main
Showing /home/bogdan/ex/bored/app/views/tasks/_rand_task.html.erb where line #3 raised:
PG::Error: ERROR: relation "tasks" does not exist
LINE 4: WHERE a.attrelid = '"tasks"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"tasks"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
Since that is the first call of an object, I'm wandering shouyld i migrate or do anyhting else in order to get my pg working (sorry if the question is dummiesh)
You need to run rake db:create:all to create your new Postgres databases.
Related
I am having a lot of problems rewriting this SQL Query in Squeel or straight Active Record using Ruby on Rails 4.
All 3 numbers in the query need to be passed to the query before execution. The SQL Query is below.
SELECT "users".*
FROM "users"
WHERE "users"."id"
IN (SELECT "users"."id" FROM "users"
INNER JOIN marketing_assets ON users.id = marketing_assets.user_id
WHERE marketing_assets.marketing_platform_id= 3
GROUP BY users.id HAVING COUNT(DISTINCT marketing_assets.marketing_platform_id) = 1)
You can find out more detail about how it is being used at Error when trying to chain class method in controller in Ruby on Rails
marketing_assets_id = 3
limit_marketing_assets = 1
User.joins(:marketing_assets).where(:marketing_assets => {:marketing_platform_id => marketing_assets_id}).group(:users => :id).having("COUNT(DISTINCT marketing_assets.marketing_platform_id) = #{limit_marketing_assets}")
Here is the query I ended up writing using Squeel. It works for me.
User.where{users.id.in(User.joins{marketing_assets}
.where((['marketing_assets.marketing_platform_id = ?' ] * platforms.count)
.join(' OR '), *platforms).group{users.id}
.having{{marketing_assets =>
{COUNT(DISTINCT(marketing_assets.marketing_platform_id)) => platforms.count }}})}
I have followed the tutorials found in the devise ldap github site. When I try to login I get this in the log:
Started POST "/users/sign_in" for 127.0.0.1 at Tue Feb 05 11:37:04 -0500 2013
Processing by Devise::SessionsController#create as HTML
Parameters: {"authenticity_token"=>"Cl2fTO6guIUMNVCV2+TXSZRNR56RFqiAurbicwyfito=", "commit"=>"Sign in", "user"=>{"remember_me"=>"0", "password"=>"[FILTERED]", "login"=>"jchoi"}, "utf8"=>"���"}
SQL (7.9ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."login" = 'jchoi' LIMIT 1
SQL (3.4ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
LDAP: LDAP dn lookup: =jchoi
LDAP: LDAP search for login: =jchoi
LDAP: Authorizing user =jchoi,ou=Root,dc=complay,dc=com
LDAP: LDAP dn lookup: =jchoi
LDAP: LDAP search for login: =jchoi
Completed in 53ms
Processing by Devise::SessionsController#new as HTML
Parameters: {"authenticity_token"=>"Cl2fTO6guIUMNVCV2+TXSZRNR56RFqiAurbicwyfito=", "commit"=>"Sign in", "user"=>{"remember_me"=>"0", "password"=>"[FILTERED]", "login"=>"jchoi"}, "utf8"=>"���"}
Rendered devise/shared/_links.erb (1.6ms)
Rendered devise/sessions/new.html.erb within layouts/application (13.8ms)
Completed 200 OK in 23ms (Views: 20.7ms | ActiveRecord
does this mean that ldap connection went through and something is wrong with my ruby code or is it not being able to access and check my username and password and is just throwing an invalid username and password error?
Thank you
Trying to search where movies coming out have a release date greater than today's date
Movie.where('release > ?', Date.today)
ActiveRecord::StatementInvalid: Mysql::ParseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release > '2011-09-25')' at line 1: SELECT `movies`.* FROM `movies` WHERE (release > '2011-09-25')
Rails 3+ :
Movie.where('release > ?', DateTime.now)
Pre Rails 3
Movie.where(['release > ?', DateTime.now])
In recent versions of rails, you can do this:
User.where(created_at: 3.days.ago..Time.now)
See some other examples here: https://stackoverflow.com/a/24150094
Update
Rails core team decided to revert this change for a while, in order to discuss it in more detail. See this comment and this PR for more info.
I am leaving my answer only for educational purposes.
new 'syntax' for comparison in Rails 6.1 (Reverted)
Movie.where('release >': DateTime.now)
Here is a link to PR where you can find more examples.
In Ruby 2.7, you can try this:
License.where(expiration: Time.zone.today..)
SELECT "licenses".* FROM "licenses" WHERE "licenses"."expiration" >= '2021-07-06 15:12:05'
Ruby beginless/endless ranges can also be used as an out-of-the-box solution:
Post.where(id: 1..)
=> Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" >= $1 [["id", 1]]
Post.where(id: ..9)
=> Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" <= $1 [["id", 9]]
Post.where(id: ...9)
=> Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" < $1 [["id", 9]]
Note:
Replace id with your date column release
Replace value with Date.today
What does the #posts line do exactly?
def index
if params[:user_id] && params[:artist_id]
#id = params[:user_id]
#name = Artist.find(params[:artist_id]).name
#posts = Post.includes(:user).where('users.id' => #id).joins(:artists).where('artists.name' => #name)
end
end
It seems to give me this really long query: confused as to why it needs all this.
Thanks
Started GET "/users/example4/artists/22/posts" for 127.0.0.1 at 2011-07-29 16:34:48 -0700
Processing by PostsController#index as HTML
Parameters: {"user_id"=>"example4", "artist_id"=>"22"}
Artist Load (0.2ms) SELECT "artists".* FROM "artists" WHERE "artists"."id" = 22 LIMIT 1
Post Load (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "posts"."content" AS t0_r2, "posts"."user_id" AS t0_r3, "posts"."created_at" AS t0_r4, "posts"."updated_at" AS t0_r5, "posts"."item_name" AS t0_r6, "posts"."a_name" AS t0_r7, "posts"."image" AS t0_r8,
"posts"."collection_id" AS t0_r09, "posts"."featured_post" AS t0_r10, "users"."id" AS t1_r0,
"users"."email" AS t1_r1, "users"."encrypted_password" AS t1_r2, "users"."reset_password_token" AS
t1_r3, "users"."remember_token" AS t1_r4, "users"."remember_created_at" AS t1_r5,
"users"."sign_in_count" AS t1_r6, "users"."current_sign_in_at" AS t1_r7, "users"."last_sign_in_at"
AS t1_r8, "users"."current_sign_in_ip" AS t1_r9, "users"."last_sign_in_ip" AS t1_r10,
"users"."created_at" AS t1_r11, "users"."updated_at" AS t1_r12, "users"."name" AS t1_r13,
"users"."username" AS t1_r14, "users"."bio" AS t1_r15, "users"."avatar" AS t1_r16,
"users"."cached_slug" AS t1_r17, "users"."bg_image" AS t1_r18, "users"."bg_tile" AS t1_r19 FROM
"posts" INNER JOIN "artisanships" ON "posts"."id" = "artisanships"."post_id" INNER JOIN "artists"
ON "artists"."id" = "artisanships"."artist_id" LEFT OUTER JOIN "users" ON "users"."id" =
"posts"."user_id" WHERE "users"."id" = 0 AND "artists"."name" = 'bobby' ORDER BY posts.created_at
DESC
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
Rendered posts/artists.html.erb within layouts/application (275.3ms)
Completed 200 OK in 481ms (Views: 316.4ms | ActiveRecord: 3.4ms)
I'd recommend reading the AR querying guide if you haven't yet.
The code you've pasted is from a controller–and in that action, you're asking for multiple things. That's why multiple queries are taking place.
This line:
#name = Artist.find(params[:artist_id]).name
is querying the artists table and getting the name attribute from the result
While this line:
#posts = Post.includes(:user).where('users.id' => #id).joins(:artists).where('artists.name' => #name)
has both an .includes and a .joins in it ... that's why all the querying is required. As long as you have proper indexes created in your table, it shouldn't be a problem.
It's using a method called eager loading. That's what the includes does.
What this means is instead of when you do
#posts.first.user
rails querying again it writes 3 queries and calls all the users for the post and loads them into memory. That way you cut down on query counts when you do something like
#posts.each do |post|
pusts post.user
The joins is the same as an SQL join
I am using postgres tsearch on a project with Rails 3.0.9. To make a tsearch query, I need to include extra SQL in my "from" clause. For example, say I have this model:
class User < ActiveRecord::Base
has_one :profile
has_many :memberships
has_many :groups, :through => :memberships
end
I want to do a fulltext search on users and their profiles. I can do this:
User.joins(:profile).where(
"(profiles.vectors ## tsearch_query) or (users.vectors ## tsearch_query)"
).from(
"to_tsquery('MYQUERY') as tsearch_query, users")
This produces the following SQL and it works fine:
"SELECT \"users\".* FROM to_tsquery('MYQUERY') as tsearch_query, users INNER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" WHERE ((profiles.vectors ## tsearch_query) or (users.vectors ## tsearch_query))"
But if I tack on another join I get some bad SQL:
User.joins(:profile).where(
"(profiles.vectors ## tsearch_query) or (users.vectors ## tsearch_query)"
).from(
"to_tsquery('MYQUERY') as tsearch_query, users").joins(:groups)
Here's the error:
ActiveRecord::StatementInvalid: PGError: ERROR: missing FROM-clause entry for table "memberships" at character 108
: SELECT "users".* FROM to_tsquery('MYQUERY') as tsearch_query, users INNER JOIN "groups" ON "groups"."id" = "memberships"."group_id" WHERE AND ((profiles.vectors ## tsearch_query) or (users.vectors ## tsearch_query))
There should be three join statements in this query. users-to-profiles, users-to-memberships and memberships-to-groups. Only the last join is included, so we get an error for referencing the memberships table without joining it earlier.
But AR::Relation does know about both joins:
irb(main)> _.send(:joins_values)
=> [:profile, :groups]
I think the problem is from adding that "from" scope call. If I cut it out, I get both my joins. For example, I can even provide a dummy "from" call and get the same error:
User.joins(:profile).from( "users" ).joins(:groups)
ActiveRecord::StatementInvalid: PGError: ERROR: missing FROM-clause entry for table "memberships" at character 68
: SELECT "users".* FROM users INNER JOIN "groups" ON "groups"."id" = "memberships"."group_id"
irb(main)> _.send(:joins_values)
=> [:profile, :groups]
Removing the "from" call, this works fine:
User.joins(:profile).joins(:groups)
irb(main)> _.to_sql
=> "SELECT \"users\".* FROM \"users\" INNER JOIN \"profiles\" ON \"profiles\".\"user_id\" = \"users\".\"id\" INNER JOIN \"memberships\" ON \"users\".\"id\" = \"memberships\".\"user_id\" INNER JOIN \"groups\" ON \"groups\".\"id\" = \"memberships\".\"group_id\"
So I'm not sure how to work around this.
My ultimate goal is to be able to do a tsearch search on User and their profile, while also limiting the results by the groups the user is in.
FWIW, this isn't a great answer, but a decent work around: I can get this to work by passing the actual SQL for the joins to User.joins, rather than using the relation names. Might have to do for now.
In the meantime I suppose I'll put together a bug report.
Another not so great solution is to wait for rails 3.1.
Added this test to activerecord's inner_join_association_test.rb:
def test_from_clause_clobbers_multiple_joins
result = Author.joins(:posts).from('authors').joins(:categorizations).where(:categorizations => {:id => 1}, :posts => {:id => 1}).to_a
assert_equal authors(:david), result.first
end
fails on 3.0.9 but passes on 3.1-rc1