I want to be able to create attributes on a ActiveRecord:Base Model that are nested.
For example -
class Book < ActiveRecord::Base
attr_accessor :operator, :who
I would like :who to have further attributes like :family, :me
So finally I can access these as follows
book = Book.new
book.who.family = [1,2,3]
book.who.me = 1
I also want to know how can I define the kind of values that attributes can take so I do not have to do that at runtime.
Currently am using something like this
after_initialize do
#who = {family: [], me: nil}
end
I tried it in my console. So if you add the your attributes as attribute accessors and after_initialize set them like this.
attr_accessor :who, :operator
after_initialize do
self.who = {family: [], me: nil}
self.operator = "Minus"
end
then you can access them like this
self.who[:family] = [1,2,3,4]
or
self.operator = "Minus"
and you can simply access them like this
self.who[:family] ==> [1,2,3]
Will this help?
Related
I would like to do the same trick as in this post - https://thoughtbot.com/blog/fast-json-apis-in-rails-with-key-based-caches-and, but it is impossible to do in AMS 0.10.0.rc because AMS doesn't have a #serializable_hash method.
What would you suggest doing instead?
Updated to the new version of active_model_serializers :
class BusinessDistanceSerializer < ActiveModel::Serializer
attributes :distance
def attributes options = {}, reload = false
data = super
data.reverse_merge! ActiveModel::Serializer.adapter.new(BusinessSerializer.new(object)).serializable_hash
data
end
end
Notice the reload = false argument.
This is how i'm doing it.
class BusinessDistanceSerializer < ActiveModel::Serializer
attributes :distance
def attributes options = {}
data = super
data.reverse_merge! ActiveModel::Serializer.adapter.new(BusinessSerializer.new(object)).serializable_hash
data
end
end
I have two models :
class Project < ActiveRecord::Base
has_many :ticket
attr_accessible ....
end
class Ticket < ActiveRecord::Base
belongs_to :project
attr_accessible done_date, description, ....
end
In my ProjectsController I would like to create a two dimensional hash to get in one variable for one project all tickets that are done (with done_date as key and description as value).
For example i would like a hash like this :
What i'm looking for :
#tickets_of_project = ["done_date_1" => ["a", "b", "c"], "done_date_2" => ["d", "e"]]
And what i'm currently trying (in ProjectsController) ...
def show
# Get current project
#project = Project.find(params[:id])
# Get all dones tickets for a project, order by done_date
#tickets = Ticket.where(:project_id => params[:id]).where("done_date IS NOT NULL").order(:done_date)
# Create a new hash
#tickets_of_project = Hash.new {}
# Make a loop on all tickets, and want to complete my hash
#tickets.each do |ticket|
# TO DO
#HOW TO PUT ticket.value IN "tickets_of_project" WITH KEY = ticket.done_date ??**
end
end
I don't know if i'm in a right way or not (maybe use .map instead of make a where query), but how can I complete and put values in hash by checking index if already exist or not ?
Thanx :)
I needed to do the same task before, following solution isn't pretty but should do it:
Ticket.where(:project_id => params[:id]).where("done_date IS NOT NULL").group_by {|t| t.done_date}.map {|k,v| [k => v.map {|vv| vv.value}] }.flatten.first
For example in my Car model i have such fields:
color, price, year
and in form partial i generate form with all this fields. But how to code such logic:
user could enter color and year and i must find with this conditions, user could enter just year or all fields in same time...
And how to write where condition? I could write something like:
if params[:color].present?
car = Car.where(color: params[:color])
end
if params[:color].present? && params[:year].present?
car = Car.where(color: params[:color], year: params[:year])
end
and so over....
But this is very ugly solution, i'm new to rails, and want to know: how is better to solve my problem?
Check out the has_scope gem: https://github.com/plataformatec/has_scope
It really simplifies a lot of this:
class Graduation < ActiveRecord::Base
scope :featured, -> { where(:featured => true) }
scope :by_degree, -> degree { where(:degree => degree) }
scope :by_period, -> started_at, ended_at { where("started_at = ? AND ended_at = ?", started_at, ended_at) }
end
class GraduationsController < ApplicationController
has_scope :featured, :type => :boolean
has_scope :by_degree
has_scope :by_period, :using => [:started_at, :ended_at], :type => :hash
def index
#graduations = apply_scopes(Graduation).all
end
end
Thats it from the controller side
I would turn those into scopes on your Car model:
scope :by_color, lambda { |color| where(:color => color)}
scope :by_year, lambda { |year| where(:year => year)}
and in your controller you would just conditionally chain them like this:
def index
#cars = Car.all
#cars = #cars.by_color(params[:color]) if params[:color].present?
#cars = #cars.by_year(params[:year]) if params[:year].present?
end
user_params = [:color, :year, :price]
cars = self
user_params.each do |p|
cars = cars.where(p: params[p]) if params[p].present?
end
The typical (naive, but simple) way I would do this is with a generic search method in my model, eg.
class Car < ActiveRecord::Base
# Just pass params directly in
def self.search(params)
# By default we return all cars
cars = all
if params[:color].present?
cars = cars.where(color: params[:color])
end
if params[:price1].present? && params[:price2].present?
cars = cars.where('price between ? and ?', params[:price1], params[:price2])
end
# insert more fields here
cars
end
end
You can easily keep chaining wheres onto the query like this, and Rails will just AND them all together in the SQL. Then you can just call it with Car.search(params).
I think you could use params.permit
my_where_params = params.permit(:color, :price, :year).select {|k,v| v.present?}
car = Car.where(my_where_params)
EDIT: I think this only works in rails 4, not sure what version you're using.
EDIT #2 excerpt from site I linked to:
Using permit won't mind if the permitted attribute is missing
params = ActionController::Parameters.new(username: "john", password: "secret")
params.permit(:username, :password, :foobar)
# => { "username"=>"john", "password"=>"secret"}
as you can see, foobar isn't inside the new hash.
EDIT #3 added select block to where_params as it was pointed out in the comments that empty form fields would trigger an empty element to be created in the params hash.
Trying to mirror my API responses with as little code duplication as possible and have this so far....
Really, this is a "There has to be a better 'Rails way' to accomplish this..." question.
class Quote < ActiveRecord::Base
belongs_to :author
has_many :votes
def as_json(options={})
hash = super(except)
hash[:author] = self.author.name
hash[:vote_count] = self.votes.count
hash
end
def to_xml(options={})
hash = super(except)
hash[:author] = self.author.name // <---- line 14
hash[:vote_count] = self.votes.count
hash
end
private
def except
{ :except => [ :id, :created_at, :updated_at, :author_id ] }
end
end
JSON response works like a champ, but the xml throws this error
can't convert Symbol into Integer
app/models/quote.rb:14:in `[]='
app/models/quote.rb:14:in `to_xml'
As a secondary question, is the the best way to customize the output like I am? I'd like to not duplicate this logic if I can avoid it.
hash[:author] = self.author.name
hash[:vote_count] = self.votes.count
hash
to_xml returns an XML string, not a hash. That's why it's surprised by a symbol in the brackets: it thinks you're trying to modify a particular character, e.g. name[0] = 'A'
If you're interested in changing bits of the XML output, maybe you should just build a new hash of the attributes you want and run to_xml on that.
In the database I have a field named 'body' that has an XML in it. The
method I created in the model looks like this:
def self.get_personal_data_module(person_id)
person_module = find_by_person_id(person_id)
item_module = Hpricot(person_module.body)
personal_info = Array.new
personal_info = {:studies => (item_module/"studies").inner_html,
:birth_place => (item_module/"birth_place").inner_html,
:marrital_status => (item_module/"marrital_status").inner_html}
return personal_info
end
I want the function to return an object instead of an array. So I can
use Module.studies instead of Model[:studies].
This is relatively simple; you're getting an Array because the code is building one. If you wanted to return an object, you'd do something like this:
class PersonalData
attr_accessor :studies
attr_accessor :birth_place
attr_accessor :marital_status
def initialize(studies,birth_place,marital_status)
#studies = studies
#birth_place = birth_place
#marital_status = marital_status
end
end
And your translation code would look like:
def self.get_personal_data_module(person_id)
person_module = find_by_person_id(person_id)
item_module = Hpricot(person_module.body)
personal_info = PersonalData.new((item_module/"studies").inner_html,
(item_module/"birth_place").inner_html,
(item_module/"marital_status").innner_html)
return personal_info
end
Or, if you want to avoid a model class, you could do something weird:
class Hash
def to_obj
self.inject(Object.new) do |obj, ary| # ary is [:key, "value"]
obj.instance_variable_set("##{ary[0]}", ary[1])
class << obj; self; end.instance_eval do # do this on obj's metaclass
attr_reader ary[0].to_sym # add getter method for this ivar
end
obj # return obj for next iteration
end
end
end
Then:
h = {:foo => "bar", :baz => "wibble"}
o = h.to_obj # => #<Object:0x30bf38 #foo="bar", #baz="wibble">
o.foo # => "bar"
o.baz # => "wibble"
It's like magic!
on a slightly different tack.
The idea of using a class method to do this feels wrong from an OO point of view.
You should really refactor this so that it works from an instance method.
def personal_data_module
item_module = Hpricot(body)
{
:studies => (item_module/"studies").inner_html,
:birth_place => (item_module/"birth_place").inner_html,
:marrital_status => (item_module/"marrital_status").inner_html
}
end
Then, where you need to use it, instead of doing....
Foobar.get_personal_data_module(the_id)
you would do
Foobar.find_by_person_id(the_id).personal_data_module
This looks worse, but in fact, thats a bit artificial, normally, you would be
referencing this from some other object, where in fact you would have a 'handle' on the person object, so would not have to construct it yourself.
For instance, if you have another class, where you reference person_id as a foreign key, you would have
class Organisation
belongs_to :person
end
then, where you have an organisation, you could go
organisation.person.personal_information_module
Yes, I know, that breaks demeter, so it would be better to wrap it in a delegate
class Organisation
belongs_to :person
def personal_info_module
person.personal_info_module
end
end
And then from controller code, you could just say
organisation.personal_info_module
without worrying about where it comes from at all.
This is because a 'personal_data_module' is really an attribute of that class, not something to be accessed through a class method.
But this also brings up some questions, for instance, is person_id the primary key of this table? is this a legacy situation where the primary key of the table is not called 'id'?
If this is the case, have you told ActiveRecord about this or do you have to use 'find_by_person_id' all over where you would really want to write 'find'?