Posts

Showing posts with the label ruby-on-rails

Ruby on Rails — Stack level too deep

Ruby on Rails — Stack level too deep model: after_save :set_correct_post_type def set_correct_post_type if self.document.present? if (find_mime_type(self.document.original_filename) == "application/vnd.openxmlformats-officedocument.presentationml.presentation") || (find_mime_type(self.document.original_filename) == "application/vnd.ms-powerpoint") self.update_attributes(:post_type => 3) elsif (find_mime_type(self.document.original_filename) == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) || (find_mime_type(self.document.original_filename) == "application/msword") || (find_mime_type(self.document.original_filename) == "application/pdf") self.update_attributes(:post_type => 2) elsif (MIME::Types.type_for(self.document.original_filename).first.content_type == "image/png") || (MIME::Types.type_for(self.document.original_filename).first.content_type =="image/jpeg...

How to fetch parents based on their child's records

How to fetch parents based on their child's records I have two models like company.rb class Company < ApplicationRecord has_many :posts end post.rb class Post < ApplicationRecord belongs_to :company scope :notClosed, -> {where(closed: false)} scope :published, -> {where(published: true)} end I want to fetch companies which carries at least one post with matching post scope scope Currently my queries is Company.where(company_type: "Private").all It's return all companies but how to modify this query for my needs. I want to fetch companies which carries at least one post , actually, an INNER JOIN keyword would return the companies with posts associated, you can append a distinct statement to get non-repeated records. Can you explain further what you're trying to do? – Sebastian Palma Jul 2 at 3:55 ...

Rails routes do not contain id in that

Rails routes do not contain id in that I use rails 5 and setting route, type rake routes in console and result is: Prefix Verb URI Pattern Controller#Action new_report_templates GET /report_templates/new(.:format) report_templates#new edit_report_templates GET /report_templates/edit(.:format) report_templates#edit report_templates GET /report_templates(.:format) report_templates#show PATCH /report_templates(.:format) report_templates#update PUT /report_templates(.:format) report_templates#update DELETE /report_templates(.:format) report_templates#destroy POST /report_templates(.:format) report_templates#create It's so weird to see that result routes do not have id in url template. My route: Rails.application.routes.draw do resource :report_templates end I use gem 'rails', '~> 5.1.6'. So my question is: why my result routes d...

Not able to add DataValidation in rubyXL while writing to excel

Not able to add DataValidation in rubyXL while writing to excel I am using rubyXL gem to write to excel sheet. I need to add a data validation for a column which has dropdown select i.e (List type). To implement this, I see in rubyXL class we have data_validation.rb data_validation.rb gems data_validation code which has type to array of values of ST_DataValidationType = %w{ none whole decimal list date time textLength custom } From this,I don't find a lead to write a dropdown list in the worksheet. Could someone guide me to implement this dropdown inside the excel sheet? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Getting errors in Rails asset:pipeline with Webpacker and Heroku

Getting errors in Rails asset:pipeline with Webpacker and Heroku I have a Rails 5.1.4 app that I inherited that is deployed to Heroku. It is using webpacker with vuejs and am now having problems deploying to Heroku. I am getting the following message when Heroku run rake assets:precompile: rake assets:precompile: remote: [13] ./app/javascript/production-guarantee-component.vue 193 bytes {0} [built] [failed] [1 error] remote: [14] ./app/javascript/packs/tmp_dashboard.js 2.44 kB {1} [built] remote: + 4 hidden modules remote: ERROR in ./app/javascript/adder-dashboard.vue remote: Module parse failed: Unexpected token (1:0) remote: You may need an appropriate loader to handle this file type. remote: | <template> remote: | <div class="col-md-12"> remote: | <adder-component v-for="category in categories" :category ="category"></adder-component> remote: @ ./app/ja...

Run local code on a Heroku production instance

Run local code on a Heroku production instance Is it possible for me to run something like: RAILS_ENV=production heroku run Item.remove_dupes where the code is local on my machine but it is run on my production Heroku instance? (ie not having pushed code to production via GitHub). 1 Answer 1 You can use Heroku console $ heroku run console Edit Heroku console will automatically run in production mode thx - just to be clear, I'd like to run code that is local (ie not on production and that hasn't been deployed) against my production instance. I am pretty sure running heroku run ... basically sends the command to the heroku server and runs it locally there. I'm doubting what I do can be done. – timpone Aug 30 '16 at 1:01 heroku run ... ...

How do I count unread comments using “Unread” Ruby gem?

How do I count unread comments using “Unread” Ruby gem? comments_helper.rb : module CommentsHelper def unread_comments_count @comments_count = Comment.unread_by(user).count end end In my ApplicationController , I defined user : ApplicationController user def user @user = User.find_by(params[:user_id]) end I could have used current_user , but the result I got (with that) is total number of comments in my database. So, I thought I should use user . But it didn't work. current_user user view : <%= unread_comments_count %> However, the result is still total number of all the comments made on all the posts by all users. I can't actually figure out what I am doing wrong because I have carefully done everything right in my Models as guided by the gem. I would appreciate any clue to fix this. Update The query it generates is: SELECT COUNT(*) FROM "comments" 'LEFT JOIN read_marks ON read_marks.readable_type = "Message" AND read_marks.readable_id ...

Node.js headless browser replacement for PhantomJS for jasmine tests

Node.js headless browser replacement for PhantomJS for jasmine tests PhantomJS development is suspended, so I need to find replacement for it. In my Ruby on Rails application Javascript Unit tests are running via jasmine-gem and they run in PhantomJS headless browser. Could you please advise alternatives for PhantomJS and ways how to incorporate them to run jasmine tests? It's off-topic question. Any way, Nightmare or headless Chrome (Puppeteer). It's unclear what this has to do with Jasmine because it's primarily intended for unit tests. For instance, Testcafe is intended for E2E and supports Chrome natively. – estus Jul 1 at 18:36 Definitely puppeteer – Vaviloff Jul 2 at 3:42 ...

Best way to save attribute inside save block?

Best way to save attribute inside save block? In my controllers I often have functionality like this: @account = Account.new(account_params) if @account.save if @account.guest? ... else AccountMailer.activation(@account).deliver_later @account.update_column(:activation_sent_at, Time.zone.now) flash[:success] = "We've sent you an email." redirect_to root_path end end What's the best way to send an email and update the activation_sent_at attribute without having to save the record twice? Calling update_column doesn't feel right to me here because AFAIK it creates an extra SQL query (correct me if I'm wrong). activation_sent_at update_column The workaround is using assign_attributes , but I would not recommend it. Your code is fine, and an additional update query to update the column shouldn't make much difference. Also, check like guest? can also be applicable on some model hooks which won't be in...