Rails has_many with dynamic conditions

Rails 4+ way (Thanks to Thomas who answered this below): has_many :faixas_aliquotas, -> (object) { where(“regra_fiscal = ?”, object.regra_fiscal) }, :class_name => ‘Fiscal::FaixaAliquota’ Rails 3.1+ way: has_many :faixas_aliquotas, :class_name => ‘Fiscal::FaixaAliquota’, :conditions => proc { “regra_fiscal = #{self.regra_fiscal}” } Rails 3 and below: has_many :faixas_aliquotas, :class_name => ‘Fiscal::FaixaAliquota’, :conditions => [‘regra_fiscal = #{self.regra_fiscal}’] No. This … Read more

Is there a way to get a collection of all the Models in your Rails app?

The whole answer for Rails 3, 4 and 5 is: If cache_classes is off (by default it’s off in development, but on in production): Rails.application.eager_load! Then: ActiveRecord::Base.descendants This makes sure all models in your application, regardless of where they are, are loaded, and any gems you are using which provide models are also loaded. This … Read more

How I can put composite keys in models in Laravel 5?

I wrote this simple PHP trait to adapt Eloquent to handle composite keys: <?php namespace App\Model\Traits; // *** Adjust this to match your model namespace! *** use Illuminate\Database\Eloquent\Builder; trait HasCompositePrimaryKey { /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() { return false; } /** … Read more

Get Android Phone Model programmatically , How to get Device name and model programmatically in android?

I use the following code to get the full device name. It gets model and manufacturer strings and concatenates them unless model string already contains manufacturer name (on some phones it does): public String getDeviceName() { String manufacturer = Build.MANUFACTURER; String model = Build.MODEL; if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) { return capitalize(model); } else { return capitalize(manufacturer) + … Read more