Hey we are 3 students, we all use the same rails db:seed. Our project is well git pulled and coordinated, but ..
One uses Linux, the rails:db:seed works for him.
One uses Mac, the rails:db:seed works for him too.
Me, I use WSL, and it dosnt work !
I've tried both Windows & WSL paths, as the screens bellow.
Thanks if anyone can guide me !
config/seed.rb
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
require 'faker'
RealEstate.destroy_all
User.destroy_all
Category.destroy_all
Category.create(title:"House")
Category.create(title:"Flat")
10.times do
u = User.create(email: Faker::Internet.email, password: Faker::Internet.password)
end
30.times do
re = RealEstate.create(
title: Faker::Space.galaxy,
description: Faker::Lorem.paragraph_by_chars(number: 256),
address: Faker::Address.full_address,
location: Faker::Address.city,
price: Faker::Number.number(digits: 8),
user: User.all.sample(),
category: Category.all.sample()
)
re.images.attach(io: File.open(ENV['SAMPLE_IMAGES']), filename: 'sample_image')
end
puts "%" * 50
puts " Base de données remplie !"
puts "%" * 50
Resolved !
I actually went to the folder where the file was stocked and simply typed " pwd " :
So, in my case the pathname was : '/home/pedrofromperu/next/images/indian.jpg'
This issue you are experiencing happens to lots of people who switch between Unix-Like and Windows operating systems. Paths in Windows are written using ‘\’ instead of ‘/‘. This is particularly confusing when using WSL and PowerShell in windows terminal - you have to keep track of which shell environment you are using. Congrats using ‘print working directory’, pwd, to solve the problem.
One thing you could do if you change environments a lot, (and this is just one approach.) Use the OS gem like so:
require 'os'
OS.windows? # returns true or false.
You could then either provide different paths or get fancy and replace the characters in your string.
Related
I have 2 databases defined in my YAML: primary and datawarehouse.
With the following code:
pp ActiveRecord::Base.connection_config
ActiveRecord::Base.connected_to(database: :datawarehouse) do
ActiveRecord::Base.connection.execute('select 1')
end
pp ActiveRecord::Base.connection_config
I would expect the output to be the same before and after the block and let it get back to the default.
However the output before gives me:
=> {:adapter=>"postgresql", :host=>"postgres", :encoding=>"unicode", :migrations_paths=>"db/migrate", :database=>"rails_devise_production"}
And the output after the block keeps the connection to the datawarehouse.
=> {:adapter=>"postgresql", :host=>"postgres-archive", :encoding=>"unicode", :migrations_paths=>"db/datawarehouse_migrate", :database=>"archive" }
How is this possible? I would expect only the code in the block to be run against the other database connection.
There's a similar question in Rails repo issues. In short, connected_to with database key is specifically for one-off connections, and you need to use the role key in your application.
The code below, stored at config/initializers/console.rb works only at first time I exec rails console CLI. When exit and enter again, no selection message is displayed, but the preview tenant selected is loaded.
if defined?(Rails::Console) || $PROGRAM_NAME.include?('spring')
tenants = Apartment.tenant_names.sort
default = tenants.first
puts "Available tenants: #{tenants.join(', ')}"
print "Select tenant (#{default}): "
tenant = gets.strip
Apartment::Tenant.switch! tenants.include?(tenant) ? tenant : default
end
I wish every time when enter at rails console ask for what tenant will be loaded.
Thanks!
The only way I could get Apartment::Tenant.switch! to work in the Rails console was by creating the following .irbrc file in the project's root directory:
IRB.conf[:IRB_RC] = Proc.new do
tenants = Apartment.tenant_names.sort
puts "Available tenants: #{tenants.join(', ')}"
print "Select tenant: "
tenant = gets.strip
unless tenant.empty?
if tenants.include?(tenant)
Apartment::Tenant.switch!(tenant)
else
puts "Tenant not found in list '#{tenant}'"
end
end
puts "Tenant set to '#{Apartment::Tenant.current}'"
end
I faced similar issue. If you are using the Apartment Gem. In your rails console you can switch between tenants by first connecting to DB and then using schema_search_path
e.g.
c = Company.connection
c.schema_search_path = "tenant1"
To check tenant has been switched use ActiveRecord::Base.connection.schema_search_path
=> "\"tenant1\""
Company is just a table in my DB.
Here is a simple code (pry version) usable at launch or while at console
`Apartment::Tenant.switch!` during `bin/rails console` using `pry`
This happens because of Spring, by default it's configured only for the development environment. Just remove it from your Gemfile and it should work as you expected.
When I have a lot of products (3000 and 22000 variants), adding new stock location takes hours because Spree is creating stock items for every variant.
During this time variants table is locked and whole system is unusable. Is there some workaround for this or maybe it was fixed in some new version of Spree?
I am using spree 2.0.3.
I face the same problem, with >400K variants, it's impossible to add a new stock location. So, I create a script in ruby and for all variants write an insert statement to a SQL file. I must create the stock location without propagate_all_variants
# lib/create_stock_items.rb
begin
file = File.open("stock_items.sql", "w")
rescue IOError => e
puts e
end
file.write("INSERT INTO spree_stock_items (stock_location_id, variant_id, backorderable) VALUES \n")
variants = Spree::Variant.all.pluck(:id)
length = variants.count
variants.each_with_index do |variant, index|
if index+1 == length
file.write("(#{stock_location_id}, #{variant}, false); \n")
else
file.write("(#{stock_location_id}, #{variant}, false), \n")
end
end
file.close
Then run bundle exec rails runner lib/create_stock_items.rb -e production. This will create a stock_items.sql file in Rails root path, and finally load that SQL directly on BD (rails dbconsole).
I know it's a little hack, but a very fast solution for me.
Update
First of all, there is no method "context". That was a word my brain made up at some point and stuck with. Obviously I should've been running .excerpt(). Second, I was running the command against the returned array, not an individual instance of the PG_Search::Document
Double mistakes, but yes the code does in fact work.
End Update
First some system info:
Ruby 1.9.3p194
Rails 3.2.13
pg_search 0.5.7
Postgres 9.2.3 (with
unaccent enabled)
I'm trying to follow the progress made in this thread: (How to show excerpts from pg-search multisearch results)
Okay so assuming the use of the following query cribbed from that post:
#query = params[:query]
PgSearch.multisearch(#query).select("ts_headline(pg_search_documents.content, plainto_tsquery('english', ''' ' || unaccent('#{#query}') || ' ''' || ':*')) AS excerpt")
returns:
=> [#<PgSearch::Document id: 7, content: "1 <p>You think water moves fast? You should see ice...", searchable_id: 2, searchable_type: "Release", created_at: "2013-03-27 18:58:52", updated_at: "2013-03-27 18:58:52">]
It successfully returns some search results but they don't have the context method at all. It's as if I just called multisearch without the select method.
I'm a newbie when it comes to SQL and Postgres so I'm not exactly sure where to start in debugging that snippet. I would love some help debugging or getting an explanation of what is happening.
Also, an aside that I think is important, I want to thank anyone who works on pg_search or responds to questions like these. You make the world a better place.
You have to select the other columns you need as well.
For example
sanitized = ActionController::Base.helpers.sanitize(params[:q])
#results = PgSearch.multisearch(params[:q])
.select(:id, :content, :searchable_id, :searchable_type)
.select(["ts_headline(pg_search_documents.content, plainto_tsquery('english', ''' ' || '#{sanitized}' || ' ''' || ':*')) AS excerpt"])
Will return the id, content, searchable_id, searchable_type, excerpt
Also notice the sanitizing. Don't want to suffer from an sql injection attack. :]
I have a condition where I need to check for the file in another server, if that file exists I need to delete from the current server. Can any body help me.
You can place a script on another server and ask it in restful way to perform that tasks for you:
http://another.server/exists/:file_name
http://another.server/delete/:file_name
but you will have to think about security aspects of this solution.
Also take a look on executing remote commands via ssh: http://bashcurescancer.com/run_remote_commands_with_ssh.html. Combined with using ssh "without password" it can be acceptable solution to run command line program that run what you need.
Just write a ruby script and do something along the line with:
require "open-uri"
file_name = "file.name"
begin
file = open("http://www.example.com/#{file_name}")
File.delete("path_to" + file_name)
p "File #{file_name} deleted"
rescue
p "File not found"
end