How to work with associaion ids like with dirty attributes? - ruby-on-rails-3

I have some Model that has_many another models. And I want to do something before save, if structure of associated models changes. Something like this:
class Foo < AR:Base
has_many :bars
before_save :do_smth, :if => bar_ids_changed? # like ActiveModel:Dirty
def do_smth
...
end
end
What is the better way to implement this?

Related

Rails query by multiple keywords

I am trying to create a custom search method for a project I'm working on. Ideally I'd like to be able to have the user input and unlimited number of keywords to filter the results by, but I'm willing to settle for a just a couple. I have my code working for just one keyword. Here is the code in my model:
class Food < ActiveRecord::Base
has_many :meal_items, inverse_of: :food_for_meal, foreign_key: 'food_for_meal_id'
has_many :user_meals, through: :meal_items
def self.search (key)
Food.where("description LIKE ?", "%#{key}%")
end
end
Here is one attempt I have made to use multiple keywords:
class Food < ActiveRecord::Base
has_many :meal_items, inverse_of: :food_for_meal, foreign_key: 'food_for_meal_id'
has_many :user_meals, through: :meal_items
def self.search (key)
keys = key.split('+')
Food.where("description LIKE ?", "%#{keys[0]}%")
AND ("description LIKE ?", "%#{keys[1]}%")
end
end
I have tried moving things in and out of parens and quotes and so forth, but can't seem to nail down the correct syntax. Any help would be appreciated.
.where takes SQL fragments, so just put the AND inside.
Food.where("description LIKE ? OR description LIKE ?", "%#{keys[0]}%", "%#{keys[1]}%")
You could do something like for n, keys:
Food.where((['description LIKE ?'] * keys.size).join(' OR '), *keys.map{ |key| "%#{key}%" })
EDIT: You probably want OR as pointed out by a commenter in my answer.

Returning Columns from Associated Table in Model.all

Summary:
I have a model with a few "belongs_to" associations and when I call Model.all (or another custom method if need be) I want to not only return all columns of Model, but also one column from each of the associated Models. Basically instead of just returning the ID's of the associated Models I want to get a more friendly attribute like "name".
Using Rails 3.2.x
Model Details:
I have five models, basically for data normalization.
class ActionItem < ActiveRecord::Base
belongs_to :action_item_status
belongs_to :prod_ops_acceptance
belongs_to :action_item_priority
belongs_to :incident_ticket
<truncated the rest>
end
class IncidentTicket < ActiveRecord::Base
attr_accessible :number
has_many :action_items
validates_presence_of :number
end
class ActionItemPriority < ActiveRecord::Base
attr_accessible :name
has_many :action_items
validates_presence_of :name
end
class ActionItemStatus < ActiveRecord::Base
attr_accessible :name
has_many :action_items
validates_presence_of :name
end
class ProdOpsAcceptance < ActiveRecord::Base
attr_accessible :name
has_many :action_items
validates_presence_of :name
end
Attempted Solutions:
I've tried many combinations of things including using ActionItem.includes and ActionItem.joins to no avail. The latest thing I tried is (trying only for the 'number' attribute of the IncidentTicket model to start with...)
ActionItem.all(
select: 'action_items.title, incident_tickets.number',
joins: 'INNER JOIN incident_tickets
ON action_items.incident_ticket_id = incident_tickets.id')
The above only returns the 'title' attribute from the ActionItem model and not the 'number' attribute from the IncidentTicket model despite the SQL looking correct. It seems like the SELECT on the joined table is completely ignored no matter what I try.
Obviously I am seriously missing something here or doing this completely wrong. I feel like there is some ActiveRecord magic that I'm missing out on that makes this trivial. Any help would be much appreciated! Please let me know if you need more details, I feel like this is kind of difficult to explain...
This ought to work for you:
action_items =
ActionItem.joins(:incident_ticket, :action_item_priority, ...)
.select(%[ action_items.title,
incident_tickets.number AS incident_ticket_number,
action_item_priorities.name AS action_item_priority_name,
... ]
)
.all
logger.info(action_items.first.incident_ticket_number)
What I ended up doing for now is creating a method that returns an array containing the results of ActionItem.all with the additional attributes I want injected in. This can probably be optimized, but I haven't spent any more time focusing on that just yet:
def self.all_with_extras
action_items_with_extras = []
action_items = ActionItem.all.to_json
JSON.parse(action_items).each do |ai|
extras = {
'incident_ticket_number' => IncidentTicket.find(ai['incident_ticket_id']).number,
'status' => ActionItemStatus.find(ai['action_item_status_id']).name,
'priority' => ActionItemPriority.find(ai['action_item_priority_id']).name,
'acceptance' => ProdOpsAcceptance.find(ai['prod_ops_acceptance_id']).name
}
with_extras = ai.merge(extras)
action_items_with_extras.append(with_extras)
end # each
action_items_with_extras
end # def

Returning associations for specific model when there is a polymorphic association in Rails 3.2

I have a polymorphic association in a Rails 3 app where a User may favorite objects of various classes.
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favoriteable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :favorites
end
class Image < ActiveRecord::Base
has_many :favorites, :as => :favoriteable
end
class Video < ActiveRecord::Base
has_many :favorites, :as => :favoriteable
end
I would like to be able return a list of just a User's favorite_images for example.
user.favorite_images #returns a list of the images associated with the user via :favoritable
I'm guessing there is a straightforward way of doing this but I haven't been able to figure it out. Let me know if you need anymore info.
Thanks!
===edit====
I know that I could retrieve what I am looking for via
favorite_images = user.favorites.collect{|f| if f.favoriteable_type=='Image'; f.favoriteable; end}
I could define an instance method for the User class and put that inside. I was wondering if there is a way to do it as some sort of has_many association. Really just because going forward it would be easier to have all that in one place.
When you created the table for Favorite you created a couple of columns favoriteable_id and favoriteable_type and you can use this information to restrict your query.
If you do user.favorites you will get all of the favorites and to restrict them to say just the images then you can do user.favorites.where(favoriteable_type: 'image') but that just gives you the favorite records and it sounds like you want the actual images. To get those you can do it by then mapping and pulling the favoriteable out. You'll likely want to include it in the query though so you don't hit the database so much. I would also make this a method on User.
def favorite_images
favorites.includes(:favoriteable).where(favoriteable_type: 'Image').map(&:favoriteable)
end

Polymorphic has_one association and multiple inheritance with Rails 3

I've seen some posts dealing with this, and am trying to determine the best solution.
Semantically, I want a Client model with a one-to-one relationship with a Survey. There are different kinds of surveys that have different fields but I want to share a significant amount of code between them. Because of the different fields I want different database tables for the surveys. There is no need to search across different types of surveys. It feels like I want the foreign key in the Client table for fast retrieval and potential eager-loading of the Survey.
So theoretically I think I want polymorphic has_one and multiple inheritance something like this:
class Client < ActiveRecord::Base
has_one :survey, :polymorphic => true
end
class Survey
# base class of shared code, does not correspond to a db table
def utility_method
end
end
class Type1Survey < ActiveRecord::Base, Survey
belongs_to :client, :as => :survey
end
class Type2Survey < ActiveRecord::Base, Survey
belongs_to :client, :as => :survey
end
# create new entry in type1_surveys table, set survey_id in client table
#client.survey = Type1Survey.create()
#client.survey.nil? # did client fill out a survey?
#client.survey.utility_method # access method of base class Survey
#client.survey.type1field # access a field unique to Type1Survey
#client2.survey = Type2Survey.create()
#client2.survey.type2field # access a field unique to Type2Survey
#client2.survey.utility_method
Now, I know Ruby does not support multiple inheritance, nor does :has_one support :polymorphic. So is there a clean Ruby way to achieve what I'm getting at? I feel like it's right there almost...
Here's how I would do this:
class Client < ActiveRecord::Base
belongs_to :survey, :polymorphic => true
end
module Survey
def self.included(base)
base.has_one :client, :as => :survey
end
def utility_method
self.do_some_stuff
end
end
Type1Survey < ActiveRecord::Base
include Survey
def method_only_applicable_to_this_type
# do stuff
end
end
Type2Survey < ActiveRecord::Base
include Survey
end

Rails 3 model mapping certain columns to different model attributes

I have the old legacy table called "DXFTACCTS", and I created Rails model "Account".
class Account < ActiveRecord::Base
set_table_name "DXFTACCTS"
end
The problem is that DXFTACCTS has fields like "XORFNAME" which I want to be "first_name" in the model, and so on. How do I "map" specific table columns to model attributes?
Thanks!
You can use the method alias_attribute like this:
class Account < ActiveRecord::Base
set_table_name "DXFTACCTS"
alias_attribute :first_name, :XORFNAME
end
alias_attribute creates the methods first_name, first_name= and first_name? which will map to the XORFNAME column in your table. However, you will NOT be able to use it in conditions like regular columns. For example:
Account.all(:conditions => { :first_name => "Foo" })
That will fail...
I think something like definition of getter and setter methods should do the trick:
class Account < ActiveRecord::Base
...
def firts_name
self[:XORFNAME]
end
def first_name= value
self[:XORFNAME] = value
end
...
end