Create a model association where the model is not a active record model rails - ruby-on-rails

I have a model called Client.
This extends not from ActiveRecord::Base but from my own wrapper class.
These objects are not stored in my own database.
I have some other models called Answer and Device for example. These are stored in my database with a client_id.
Now, what I want to do is that is can call Answer.client and Client.answers for example. The normal way would be with ActiveRecord associations but that doesn't work is this case.
I can define my own .answers and .client method but in my opinion that's not the way to go with Rails.
Thanks in advance for replying!

Related

Ruby: inheritance or something else

I've a quick and quite basic question to ask.
I would like to create a new model which has a parameter that can be one of several model types.
Ex: the param 'targeted_object' can be either an instance of Model A or an instance of Model B.
For the moment I don't think I need a similar behavior for Model A and Model B, so my first guess is to create a Master model for Model A and Model B named TargetableObject: create inheritance.
But is it the best way to do this or I need to make something else regarding that I presume for now no related behavior for Master object children?
Thanks
If I understand correctly, Polymorphic associations could be what you need.
From the rails guides:
With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model.

Rails 'multiple inheritance' model- ActiveRecord object with a property which is another class?

I'm probably asking for the wrong thing. What I'd like is a model which extends the class ActiveMerchant::Shipping::Location, but is also saved in the database and has other neat active record associations like multiple employees, working day schedules, etc.
I know I can't inherit from multiple classes- what's the best way to create a Location class with both ActiveMerchant::Shipping::Location properties & methods and additional ActiveRecord properties & associations?
You can inherit from ActiveMerchant::Shipping::Location and include ActiveModel::Model and/or other ActiveRecord modules you desire.

Remote data model in Rails

i have model with validations, some methods, filters and so. Unfortunately the data are from API, so i need to overload the method which is pull the records from DB. Which method is that?
So far i have creation. Method create in active record's model is presisting new record. I just add method create to my model and it's creating records over the API. Now i want it for selecting the data.
Following code is example of what i already have (creation of records):
def create
EmployeesApi.create(self.attributes.reject{|k,v| %w(id created_at updated_at).include? k })
end
I need it as low level as possible, because it has some relations and app specific validations. Moving also the relations and surrounding logic would mean integrate two existing systems and that's not ok in this case.
Another explanation:
I want to bypass the database for data of the model, but for association let everything as it was. The model's data are stored in another app/database/system.
I'll load model's own data by API and ActiveRecord will pair/load it's associations from local DB
If EmployeeApi would be modeled after ActiveResource you could possibly enhance it with ActiveModel. Associations might work only in one way (from ActiveRecord to ActiveResource). It is also good to consider exceptions such as API is down.
I think what you are trying to achieve is to pull some data from the DB before creating this record? or validate some DB field, maybe ensure a unique constrain or something.. Please look into the active record hooks/callbacks
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
You can do something like this:
class Subscription < ActiveRecord::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end

Rails 3 ActiveRecord API: .build method

I am fairly new to Ruby/RoR (outside of a year) and I have noticed that there are several different methods inside of RoR or Ruby that basically do the same thing. The one method I am wanting to get some sort of clarification on, is the .build method. when it is effective to use or how to use it in its best light, sorta thing.
Thanks!
The .build method is an ActiveRecord method which is used to create a new record based on the has_many relationship in your model.
So lets say;
User has_many tweets
Then you can use
user.tweets.build(tweet_id)
This will create a new tweet in the tweets table associated with that user. It will also return that object too.
You probably want to put a params tweet_id in you argument depending on how you are implementing the app. :)

Rails 3 Polymorphic Association between one MongoMapper model and one/many Active Record model/s

I have a Record model (Active Record) that stores some custom logs.
Record is in polymorphic association with all the other model in my app, and I can effectively log what I want hooking my Record methods in the other controllers.
What I need:
To have the logs in a separate database.
So I have to:
Be able to manage two different databases in my apllication (one is Postgres/ActiveRecord and the other one is MongoDB/MongoMapper)
Generate a polymorphic association between my Record model, now with MongoMapper, and the rest of my Active Record models.
That way I can persist my logs to the MongoDB database.
Thanks.
Yes this can be done.
To create a polymorphic association you need both the class and an id. Idiomatically the fields will be named <assoc>_type and <assoc>_id‡. You will need to do some wiring up to make everything work.
Create a MongoMapper::Document Class with the keys <assoc>_type and <assoc>_id with the correct types (I believe MongoMapper allows Class as a key type) along with any other keys you may need.
Define the method <assoc> and <assoc>=
def assoc
assoc_type.find(assoc_id)
end
def assoc=(input)
assoc_type = input.class #STI makes this more complicated we must store the base class
asspc_id = input.id
end
Possibly add a method to your ActiveRecord models allowing them to access you MongoMapper logging class. If there are a lot, you may want to build a module and include it in all the classes that need that kind of functionality.
‡ replace with something meaningful for you application like 'reference' or 'subject'

Resources