ruby-on-rails-5

ActiveModel::UnknownAttributeError: unknown attribute when seeding belongs_to relationship

守給你的承諾、 提交于 2019-12-13 03:32:19
问题 Trying to seed a has_many/belongs_to relationship. A user has_many scripts, a script belongs_to a user. When seeding the script, I get this error: ActiveModel::UnknownAttributeError: unknown attribute 'user_id' for Script. I'd think that this line in my migration would create a user_id attribute for script? t.belongs_to :user, index: true, foreign_key: true app/models/script.rb class Script < ApplicationRecord belongs_to :user has_many :commits has_many :script_users, through: :commits,

must specify an iv attr_encrypted, how to check login and password

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 03:29:47
问题 Hi have the following model: class User < ActiveRecord::Base secret_key = ENV['DB_COL_ENCRYPTED_KEY'] attr_encrypted :email, :key => secret_key attr_encrypted :password, :key => secret_key [...] end Where I added the 4 cols to my model rails g migration AddEncryptedColumnsToUser encrypted_email:string encrypted_password:string encrypted_email_iv:string encrypted_password_iv:string Now I want to check if email and password are correct, but I don't know how to process: secret_key_data = "my big

Rails save massive hash data to database

戏子无情 提交于 2019-12-13 03:27:43
问题 I have a Hash with 2M data. I need to transfer all data from hash to database . Hash keys fit with database columns. I'm not sure the way I use below is the best way. # users is a hash with 2M data. users.each do |u| user = User.new(u) user.save! end what is the best way to iterate through this kind of massive data with Rails? 回答1: ActiveRecord.create accepts hashes too. Therefore I would just try: User.create(users) 2M might be a bit too big and you might need to work in batches. But I

Devise_token_auth conflicts?

谁说胖子不能爱 提交于 2019-12-13 00:59:58
问题 Made a new API in rails 5 using default --api tag and installed devise_token_auth gem using command rails generate devise_token_auth:install User auth . On doing rake:db:migrate , I get this error undefined method 'devise' for User (call 'User.connection' to establish a connection) which is weird because devise_token_auth is built on top of devise.. So, commenting out routes throws error to user.rb file containing devise :database_authenticatable, :registerable, :recoverable, :rememberable,

rails 5 enum where “like”

旧街凉风 提交于 2019-12-12 22:43:36
问题 I'm trying to query an activerecord model enum and use the like operator in the where method, and it just doesnt work. Is there some trick to allow me to query an enum this way? Works fine on regular columns. Here it is in the console. Regular string column (title) works as shown below irb(main):092:0> Proposal.select(:id,:department,:status).where('title like "test%"') Proposal Load (0.3ms) SELECT "proposals"."id", "proposals"."department", "proposals"."status" FROM "proposals" WHERE (title

How do I use the parser gem to extract this code snippet I want?

喜欢而已 提交于 2019-12-12 18:04:02
问题 I just stumbled across this Parser gem. What I want to do is extract a specific method from a long file. One example is this has_many method from Rails: def has_many(name, scope = nil, options = {}, &extension) reflection = Builder::HasMany.build(self, name, scope, options, &extension) Reflection.add_reflection self, name, reflection end The only issue is, I need to be able to also extract another version of that same method (from an earlier time), that is more verbose and complex. That

Rails 5 render a template located outside of the 'views' directory

余生颓废 提交于 2019-12-12 17:55:41
问题 The following code def show render File.join(Rails.root, 'app', 'themes', 'default_theme', 'template'), layout: File.join(Rails.root, 'app', 'themes', 'default_theme', 'layout') end Works in rails 4.2.x but outputs the following error when using Rails 5.0.0 ActionView::MissingTemplate: Missing template vagrant/app/themes/default_theme/template with {:locale=>[:nl], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in: * "/vagrant/app/views/themes

Rails 5.2 Active Storage with Cocoon forms

白昼怎懂夜的黑 提交于 2019-12-12 16:33:24
问题 I want to save some images to a model using a dynamic cocoon form with Active Storage to handle the files. I have a farmer class that has many apples, the farmer can add multiple images for each of the different kinds of apples through the farmer form. class Farmer < ActiveRecord::Base has_many :apples, inverse_of: :farmer accepts_nested_attributes_for :apples, allow_destroy: true, reject_if: :all_blank end class Apple < ActiveRecord::Base has_many_attached :apple_images end Inside the Farmer

How to setup assets pipeline in rails 5 to use a custom font?

北慕城南 提交于 2019-12-12 15:25:51
问题 I have to use a custom font Jameel Noori Nastaleeq in a rails 5 app. I have never worked with assets pipeline in rails before. Here are some of the steps I have taken: create fonts folder in app/assets , and place the extracted ttf font file in it add config.assets.paths << Rails.root.join("app", "assets", "fonts") in config/application.rb add the following in app/assets/stylesheets/couplets.scss : @font-face { font-family: 'Jameel Noori Nastaleeq'; src:asset-url('JameelNooriNastaleeq.ttf')

rails 5 capybara test.log

廉价感情. 提交于 2019-12-12 12:23:59
问题 I'm having trouble with a few test and I would like to see in test.log the requests there're being done. There's a config that I can change to register the requests in log like below? Started GET "/carts" for 127.0.0.1 at 2017-07-25 19:09:55 -0300 Processing by CartsController#show as HTML Rendering carts/show.html.erb within layouts/application Rendered layouts/_header.html.erb (1.5ms) Rendered layouts/_notice_modal.html.erb (1.2ms) Rendered carts/show.html.erb within layouts/application (8