ruby-on-rails

Rails 3 has_many changed?

可紊 提交于 2021-02-18 05:31:26
问题 I need to track changes (additions and deletes) of an association set up like this: has_many :listing_services has_many :services, through: :listing_services For normal attributes the easist way to do it is to check l.changes[attribute] in before_save or l.previous_changes[attribute] in after_save . The question is, what is the best way to do it for has_many attributes? 回答1: I didn't use changes method. But I'm sure that you always can use magic methods <attribute_name>_changed? and

How to align input and label from collection_check_boxes?

戏子无情 提交于 2021-02-18 01:57:00
问题 I am using collection_check_boxes and have problems with aligning checkbox and text. This is my code: <div class="col-md-4"> <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %> </div> Form is displayed like this: [checkbox1] text1 [checkbox2] text2 [checkbox3] text3 I am trying to align input and label but didn't have success. I have seen these question but it don't work for me: Align checkboxes for f.collection_check_boxes with Simple_Form I want to accomplish this: [checkbox1]

New Ruby on Rails Setup : “Expected string default value for '--rc'; got false (boolean)”

余生颓废 提交于 2021-02-17 21:31:51
问题 I'm setting up a Ruby on Rails web development environment on a new machine (macOS Sierra v. 10.12.1). I'm following the setup instructions here: Setup Ruby On Rails on macOS 10.12 Sierra. When I check the newly installed rails version, I get the following: $ rails --version Expected string default value for '--rc'; got false (boolean) Rails 4.2.6 I haven't seen that second line before, and googling hasn't yielded any helpful results. Background information: clean install of macOS 10.12.1;

how do you test that Rails mailer credentials are valid?

╄→гoц情女王★ 提交于 2021-02-17 19:46:29
问题 I've got a mailer sending through a GMail account, and I want to test that ActionMailer can actually log in to GMail's SMTP server with the credentials I've given it. What's the best way to test this? 回答1: It's not a full-stack solution, but you can check that the server authentication happens correctly by using Net::SMTP directly. The Mail gem that Rails 3 uses to send ActionMailer emails uses Mail like so (with your ActionMailer.smtp_settings): #line 96 of mail-2.2.7/lib/mail/network

Rails - only find records where has_many associated records are present

核能气质少年 提交于 2021-02-17 19:21:26
问题 I have a model with a has_many relation with another model like so: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end Since there are some parents that may not have children, I'd like to do a query that returns only the parents that DO have children. How would I go about something like this? Since I'm doing this on Rails 3, it would be helpful if this query were to not use the where.not. syntax. 回答1: Rails 4 Parent.includes(:child

Rails first_or_create ActiveRecord method

我怕爱的太早我们不能终老 提交于 2021-02-17 18:46:21
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

Rails first_or_create ActiveRecord method

做~自己de王妃 提交于 2021-02-17 18:40:30
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

How to crop image on upload with Rails, Carrierwave and Minimagick?

只谈情不闲聊 提交于 2021-02-17 16:58:47
问题 I have read: Undefined Method crop! Using Carrierwave with MiniMagick on rails 3.1.3 Carrierwave Cropping http://pastebin.com/ue4mVbC8 And so I tried: # encoding: utf-8 class ProjectPictureUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end version :thumb do process :cropper # process :crop process resize_to_fit: [200, 200] end def cropper manipulate! do |img| # if model.crop

Organise Active Storage files for use in other systems?

感情迁移 提交于 2021-02-17 05:58:04
问题 Active Storage stores uploads in a file structure like so: This is great if the rails app is the only software that needs to use these files. But what if the app simply serves as a means to upload the images to S3 so that some other (completely separate) service can consume them? The problem being, other developers wouldn't be able to make any sense of the directory and files, since they're labelled in a way rails can understand, but which a human cannot (e.g. what does folder named "O2" mean

Rails: Multiple dropdown menus collection_select

橙三吉。 提交于 2021-02-17 05:05:32
问题 Super Rails n00b here: Currently I have a form with the following code: <%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %> and it currently works how I want it to but now I would like to have multiple drop down menus so I can select multiple accounts. I do not want the multiple select to be on the same dropdown. If I do this: <%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %> <%= f.collection_select :account_ids, @accounts, :id