Rails 4 order by virtual attribute
If you do not put the value in a column or express the logic in search_result_value
in pure SQL, then you’ll have to load all Products into memory and then sort them in Ruby using sort_by
:
Product.all.to_a.sort_by(&:search_result_value)
Virtual Attribute in Rails 4
Instead of using attr_accessor
you could create custom getter/setters on your product model. Note that these are not backed by an regular instance attribute.
Also you can add a validation on the supply
association instead of your virtual attribute.
class Product < ActiveRecord::Base
belongs_to :supply ,dependent: :destroy
validates_associated :supply, presence:true
# getter method
def quantita
supply
end
def quantita=(val)
if supply
supply.update_attributes(value: val)
else
supply = Supply.create(value: val)
end
end
end
In Ruby assignment is actually done by message passing:
product.quantita = 1
Will call product#quantita=
, with 1 as the argument.
Another alternative is to use nested attributes for the supply.
class Product < ActiveRecord::Base
belongs_to :supply ,dependent: :destroy
validates_associated :supply, presence:true
accepts_nested_attributes_for :supply
end
This means that Product
accepts supply_attributes
- a hash of attributes.
class ProductsController < ApplicationController
#...
before_action :set_product, only: [:show, :edit, :update, :destroy]
def create
# will create both a Product and Supply
@product = Product.create(product_params)
end
def update
# will update both Product and Supply
@product.update(product_params)
end
private
def product_params
# Remember to whitelist the nested parameters!
params.require(:product)
.allow(:foo, supply_attributes: [:foo, :bar])
end
# ...
end
Yii2: How to order a relation by virtual attribute?
You can create a database view, e.g. team_score. The view should contain all the fields from the team table and a calculated field score (calculated with SQL commands). Then you can create a model TeamScore, which you can quite easily order by the field score.
Disadvantage: The values will be by every calling the view new calculated, what requires more compute power, than when the calculated values are directly stored in the table.
Advantage: It is simple to implement and firstly for displaying tables with many relations it saves a lot of coding.
How to order a controller by a virtual attribute that takes a param?
In the model you've got last_friend_udpate
, in the post params you have last_friend_udpate
, but in the index you have last_friend_update
Related Topics
Ide Sublime2 How to Find Method Definition
How to Output My Ruby Commandline Text in Different Colours
Is It a Bad Idea Do Divide the Models into Directories
Differencebetween Gsub and Sub Methods for Ruby Strings
Generate Array of All Letters and Digits
Create a Ruby Method That Accepts a Hash of Parameters
Using a Duration Field in a Rails Model
Rails Byebug Did Not Stop Application
How to Get Unique Elements in This Array
How to Understand Strptime VS. Strftime
How to Target a Specific Commit Sha with Capistrano Deploy
Rails Console Is Not Outputting SQL Statements to My Development Log
Download All Gems Dependencies