Reusable relationships - orm

What is the best/standard way to create a relationship you can add to multiple dataobjects? using a DataExtension you can create a has_many like so
public static $has_many = array('Links' => 'Link');
but then in the mirrored has_one relation in Link you would have to explicitly list the classes using the DataExtension.

Use a many_many instead of as has_many.

Have a look at the File and Image classes to get an idea of how the framework does this. The framework's unit tests for those classes will also provide an indication for how they are set up and used.

Related

Multiple table inheritance Rails and activerecord

I'm trying to implement Multiple Table Inheritance with ActiveRecord. Looks like all the available gems are pretty old. Am I missing something? Is there any "native" way to implement this with activerecord?
I'm using Rails 3.2.3 and activerecord 3.2.1
Rails 6.1+ delegated type
Rails 6.1 added a "native" way to implement Multiple Table Inheritance via delegated type.
See the corresponding PR for details.
With this approach, the "superclass" is a concrete class that is represented by its own table, where all the superclass attributes that are shared amongst all the "subclasses" are stored. And then each of the subclasses have their own individual tables for additional attributes that are particular to their implementation. This is similar to what's called multi-table inheritance in Django, but instead of actual inheritance, this approach uses delegation to form the hierarchy and share responsibilities.
Single Table Inheritance (where each Car and Truck share one database)
class Vehicle < ActiveRecord
end
class Car < Vehicle
end
class Truck < Vehicle
end
In your case you are not sharing the database but rather the functions. You should then write a module and include it in each model
class Car < ActiveRecord
extend VehicleFinders
end
class Truck < ActiveRecord
extend VehicleFinders
end
module VehicleFinders
def find_purchased
#...
end
end
So in extend module's method are class method on that calling class.include module's methods are instance method for object of calling class
This might be a good read for you http://raysrashmi.com/2012/05/05/enhance-rails-models

One abstract rails model

In rails/activerecord, is it possible to have three ruby classes, two of which inherit from one main class, and then have two separate tables for pots and pans. Like so...
class Tupperware < ActiveRecord::Base
end
class Pot < Tupperware
end
class Pan < Tupperware
end
and the advantage would be that I could use the Tupperware.find() method, and a few other things, without having to customize for each different type.
I know for sure it works with mongoid – I've done it myself a couple of times. I'm not sure if this would work in relative database engine...
But you're actually asking a question you could answer yourself, by just trying to do what you said.
[In response to OP's comment]:
I'm just saying you should do a test rails application using a relative db, such as mySQL or SQLite and define your models exactly the way you think.
I have an abstract model I use in my application. It's working perfectly and the find() method works just as you'd expect, but I'm working on Mongoid, so I don't use ActiveResource and can't say for sure if this will work for you. The only thing you can do is try.
Here, take a look at this excerpt from my code:
https://gist.github.com/ellmo/5262681

Rails active record model as a mixin

Anyone know how to define a active record model using include mixin vs class inheritance (class Car < ActiveRecord::Base).
I cant find the syntax anywhere, but I know it was introduced in Rails 3.1/3.2
Are you referring to something like this?
class Car
include ActiveModel::Model
end
although this will not give you persistence. What are you trying to do exactly?

Rails Controller: Is it good practice to define controller for associations?

Suppose i have mapping table map_user_roles. I have defined models Role and User. Both are associated to each other by relationship has_and_belongs_to_many. Of course it does not make sense to define model for mapping table in rails.
I have defined users_controller and roles_controller for crud operations on user and role respectively.
For association of user-role, what should i do? Should i define separate controller like user_roles_controller or should i make modifications in Role and User controller(if so how to do so) ?
Please suggest, what is good practice. Examples and good links would be great help
Thanks for devoting time.
I don't see what a separate controller for the association would offer that couldn't be achieved with your existing UsersController and RolesController. Also, note that sometimes is does make sense to define a model for the mapping table, that's what the has_many :through association is for. You should use it if you need to store extra attributes against the join model.

Inheritance Design

Right now, I have the following design:
Item
|------------------------|
ImageUploader |
|--------------| |
| | |
AvatarUploader FaviconUploader NameChanger
These classes are various items that users can purchase and use.
However, I'm adding a new class IconUploader. Unlike the other classes, this is not an item that can be used, but an administrative panel. ImageUploader contains certain security checks to make sure only safe files are uploaded, and IconUploader needs these precautions as well.
However, I'm not sure what to do. Ideally, I'd make ImageUploader an interface, but you can't have actual code in interfaces, so I can't do that. I could move ImageUploader out of the Item class hierarchy and make its functions static, but that doesn't feel right to me. Is it? And if it isn't, what is the best way to restructure this?
N.B.: I'm using PHP, if that affects anything.
You should probably have a GenericUploader, and then each type of uploader can specify a list of valid files, then the GenericUploader will ensure those requirements are met (along with anything else you might wish it do to).
You need a ImageUploaderBase class which is abstract and have the common functionality.
You could move the security checking and common upload functionality for the uploaders into a separate Uploader class, and make ImageUploader use this Uploader class (so all derived classes like Avatar/FaviconUploader would use it as well).
Then you could make IconUploader use Uploader (without it being derived from Item).
Then you can make ImageUploader and IconUploader implement an IUploader interface, and have all calls to the interface forwarded to the Uploader class to implement.