Rails Model has_many with multiple foreign_keys

Found a simple answer on IRC that seems to work (thanks to Radar): class Person < ActiveRecord::Base belongs_to :father, :class_name => ‘Person’ belongs_to :mother, :class_name => ‘Person’ has_many :children_of_father, :class_name => ‘Person’, :foreign_key => ‘father_id’ has_many :children_of_mother, :class_name => ‘Person’, :foreign_key => ‘mother_id’ def children children_of_mother + children_of_father end end

The better way to pass the foreign_key value to the Rails controller

You may want to consider reading the Rails Guide on nested resources: http://guides.rubyonrails.org/routing.html#nested-resources In a nutshell: routes.rb resources :galleries do resources :pictures do end # Generates the routes: /galleries/:gallery_id/pictures pictures_controller.rb def new @gallery = Gallery.find(params[:gallery_id]) @picture = Picture.new end def create @gallery = Gallery.find(params[:gallery_id]) # gallery_id is passed in the URL @picture = @gallery.build(params[:picture]) if … Read more

Rails attachments inline are not shown correctly in gmail

After all i found a solution: all you need to do is set the mime-type and encoding of the attachment. attachments.inline[‘blank’] = { :data => File.read(“#{Rails.root.to_s + ‘/app/assets/images/blank_500x500.png’}”), :mime_type => “image/png”, :encoding => “base64” } attachments.inline[‘discount-deal-triangle’] = { :data => File.read(“#{Rails.root.to_s + ‘/app/assets/images/discount-deal-triangle.png’}”), :mime_type => “image/png”, :encoding => “base64” } That did the trick for … Read more

heroku – Missing required arguments: aws_access_key_id, aws_secret_access_key, following Hartl tutorial

Go on Heroku, on your application, go to settings, hit Reveal Config Vars. Click on on Edit on the right side and enter your secrets there: S3_BUCKET: name of your bucket goes here S3_ACCESS_KEY: xxxxx S3_SECRET_KEY: xxxx On config/initializers/carrierwave.rb or wherever you’re entering your secrets should have: CarrierWave.configure do |config| config.root = Rails.root.join(‘tmp’) # adding … Read more

Combine two named scopes with OR (instead of AND)

I couldn’t find any simple solutions, but this problem intrigued me, so I rolled my own solution: class ActiveRecord::Base def self.or_scopes(*scopes) # Cleanup input scopes.map! do |scope| scope = scope.respond_to?(:to_a) ? scope.to_a : [*scope] scope.unshift(scope.shift.to_sym) end # Check for existence of scopes scopes.each{|scope| raise ArgumentError, “invalid scope: #{scope.first}” unless self.scopes.has_key?(scope.first) } conditions = scopes.map do … Read more

Ruby on Rails: Advanced search

You can create a new controller called search. Your search form: <%= form_tag search_index_path, method: :get do %> <%= text_field_tag :project, params[:project] %> <%= text_field_tag :client, params[:client] %> <%= submit_tag “Search”, name: nil %> <% end %> incude in your routes.rb: get “search/index” your search controller: def index #store all the projects that match the … Read more