ruby-on-rails-4

Display SQL queries in log with Rails 4

╄→尐↘猪︶ㄣ 提交于 2020-01-29 02:55:50
问题 I'm using Rails 4.0.4 with Ruby 2.1 and Thin 1.6.2 on Ubuntu 14.04 through my terminal "Terminator" and my shell "Fish Shell". When I'm launching my Rails server in development mode I don't have the SQL queries in my logs only the JS and HTML files are loaded. I'm searching for something like that: User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1 (2.0ms) SELECT COUNT(*) FROM "users" WHERE (driver_register_state_cd = -2) 回答1: the rails

How to create a Rails 4 Concern that takes an argument

梦想的初衷 提交于 2020-01-28 06:30:13
问题 I have an ActiveRecord class called User. I'm trying to create a concern called Restrictable which takes in some arguments like this: class User < ActiveRecord::Base include Restrictable # Would be nice to not need this line restrictable except: [:id, :name, :email] end I want to then provide an instance method called restricted_data which can perform some operation on those arguments and return some data. Example: user = User.find(1) user.restricted_data # Returns all columns except :id,

How to use i18n with Rails 4 enums

懵懂的女人 提交于 2020-01-26 15:16:19
问题 Rails 4 Active Record Enums are great, but what is the right pattern for translating with i18n? 回答1: I didn't find any specific pattern either, so I simply added: en: user_status: active: Active pending: Pending... archived: Archived to an arbitrary .yml file. Then in my views: I18n.t :"user_status.#{user.status}" 回答2: Starting from Rails 5, all models will inherit from ApplicationRecord . class User < ApplicationRecord enum status: [:active, :pending, :archived] end I use this superclass to

Solve query sql with rails

微笑、不失礼 提交于 2020-01-26 04:39:04
问题 I need that when I create a Historic do it: Table historic id authorization_origin_id 1 2 2 3 How you see in image bellow This is my new.html.erb <%= simple_form_for(@refinancing) do |f| %> <div class="form-inputs"> <%= f.hidden_field :employee_id, value: @employee.first.id %> <%= f.input :contract_number %> </div> <h3>Reserved value</h3> <table class="table table-condensed table-bordered table-striped"> <thead> <th>Authorization id</th> <th>Contract number</th> </thead> <% @authorizations

Get and display all values from database row separately based on select (Rails)

送分小仙女□ 提交于 2020-01-25 20:30:34
问题 Hi i'm stuck making an invoicing application. I want to have an option where you can add an existing customer from the customers table and add those values to the invoice. The values i want to display are company_name , vat_number and iban_number . I tried doing this: <%= select_tag 'choose customer', options_from_collection_for_select(current_user.customers.all, 'id', 'company_name') %> But obviously this only gets the value of company_name . I tried using collection.select but that one also

Setup for an uploader (carrierwave)

天大地大妈咪最大 提交于 2020-01-25 11:00:06
问题 I have an image uploader in place following a tutorial using the gems carrierwave and fog. Now I would like to add an additional uploader but am struggling. I have generated the uploader ( rails generate uploader name ). In the model file I have mounted the uploader to the right column ( mount_uploader :column_name, nameUploader ). In the uploader itself I have set def extension_white_list and store_dir . Also I included (since in the tutorial I did the same): if Rails.env.production? storage

Ruby on Rails named class property of model

泄露秘密 提交于 2020-01-25 10:10:22
问题 I have two classes Product and user which have a has_many relationship with a polymorphic type as such. class User < ActiveRecord::Base has_many :pictures, as: :imageable end class Product < ActiveRecord::Base has_many :pictures, as: :imageable end class Picture < ActiveRecord::Base belongs_to :imageable, polymorphic: true end I want to also add a new property to the user models of profile_picture so @user.profile_picture will return a picture object. how can this be achieved? In particular

Merge record into ActiveRecord Relation

╄→尐↘猪︶ㄣ 提交于 2020-01-25 06:34:05
问题 I pull a record as: def self.imp_broadcast_preview! Broadcast.where(for_gamers: true).order(:created_at).last end And then in my controller I have: def index @conversations = Conversation.where(gamer: @gamer) @conversations << Broadcast.imp_broadcast_preview! end The above code works properly in Rails 4.2 and merges the last broadcast message in the conversations. I just updated my codebase to Rails 5.2 and now I am getting an error: NoMethodError (undefined method `<<' for #<Conversation:

Do we need to permit ruby virtual attributes also?

天大地大妈咪最大 提交于 2020-01-25 03:17:20
问题 Till now I thought, I have to permit only those attributes which I required to save in database. But recently I used Jcrop to crop my User avatar and it has 4 virtual attributes which will be sent after crop from the front end, Here is my model class User < ActiveRecord::Base mount_uploader :avatar, AvatarUploader attr_accessor :crop_x, :crop_y, :crop_w, :crop_h after_update :crop_avatar def crop_avatar avatar.recreate_versions! if crop_x.present? end end When I submit after crop, my console

Do we need to permit ruby virtual attributes also?

狂风中的少年 提交于 2020-01-25 03:17:15
问题 Till now I thought, I have to permit only those attributes which I required to save in database. But recently I used Jcrop to crop my User avatar and it has 4 virtual attributes which will be sent after crop from the front end, Here is my model class User < ActiveRecord::Base mount_uploader :avatar, AvatarUploader attr_accessor :crop_x, :crop_y, :crop_w, :crop_h after_update :crop_avatar def crop_avatar avatar.recreate_versions! if crop_x.present? end end When I submit after crop, my console