Rails active record model as a mixin - ruby-on-rails-3

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?

Related

Class diagram - multiple classes uses same class

I am designing a class diagram for an assignment. In this design, I use a separate class called Currency, to define currency values and their functionality. there are at least four other classes have to use this Currency class.
How can I show it in the class diagram ? I mean, do I need to draw relationships (connecting lines) from the Currency class to all the others ?
Is there a better way ?
What am I doing wrong here ?
There is nothing wrong and a reusability of a class is valuable. Actually that's a standard situation.
If you use this class in another class as an attribute you have two options to depict that:
draw an association relationship (line) from the class using to the class that is used.
put the attribute in a proper compartment of a class that is using and as a type of an attribute (after a colon) put the name of the used class.
The benefit of the first approach is that you immediately see the dependency between the classes.
If you use a class but not directly as an attribute type you use other relationship types that suit best to the situation you want to describe.
As I imagine one of your concerns is that you'll have a lot of relationships pointing to your class (in your case Currency). Don't worry about that. You don't have to put everything in a single diagram. Put a full specification of your class on one diagram with those relationships where it uses something else and then put only the class box with a name (without any compartment) on diagrams defining those elements that use your class. It will make your model readable. And with a support of some CASE tool you will be able to see all relationship and dependencies of this class anyway. By the way that's how the UML specification is written. Look for example how Namespace is used in the diagrams there (and many others as well).
Of course I'm not suggesting creating one diagram per one element to define it. No. Collect them in logical Packages (hey - that's exactly what Packages are for!) and make a class diagram per Package. If the Package becomes too large - you might need to split it into smaller subpackages.
For Currency your Package would be probably something like Utils. It can also contain other elements like Date, Address etc. Note - these are typical examples, probably every analyst/designer/programmer sooner or later has to cope with those elements. If you build them well, you'll be really able to reuse them in future applications as well.
One last thought. While you build "package based" Class diagram you might also need a diagram that shows just specific parts coming from several Packages to clarify some bit of your system/business/whatsoever. This is also absolutely fine. Again a benefit of CASE tool here is that it keeps consistency in your model.

Should method go in class X or class Y?

This is an opinion-based question, so chances are it will be deleted.
Let's say I have class Teacher and class Course. I want to create method GetCourseId(TeacherId) that will receive as input parameter a TeacherId and will return a CourseId.
Should this method be in class Teacher or class Course?
I guess my question is that if there's a method that can fall under any number of classes, where should it finally go? Is there some unspoken rule for that?
Thanks.
I've often seen a third class created to handle something like this, where a method requires knowing about 2 classes and it doesn't quite fit in either.
In this case, it'd be the creation of a CourseManager that could contain methods like GetCourseId, GetCourseByTeachers, AddCourse, and other 'admin' tasks.
Many of these would serve as a wrapper of sorts -- CourseManager.AddCourse would likely pass a lot of work off on the Course constructor.
Normally i define classes like Teacher, Course as java beans which just hold fields, getters/setters and some very basic methods which directly use the fields and don't include any business logic.
Based on the supported functionalities/features in my application, i create business/manager classes which implement my business through communicating with the other java beans.
So if i'm creating a simple course registration application for a university, I would define 3 java beans: Teacher, Course, Student in addition to some manager classes based on the features that i want to support in my application i.e. in our case RegistrationManager which would hold methods like: registerStudentInCourse(), getStudentCourses(), addCourseTeacher() ..
Please note that I'm just sharing my way of coding, people may or may not agree with it.
The simplest solution will be having a property like
private Course course
or
private Set<Course> courses
based on cardinality (OneToOne or OneToMany) in Teacher class. It could be ManyToMany as well depends on the requirement and data modeling. You can get or set course/s assigned to the teacher using getter/setter method.
Apart from this if the relation is bidirectional than you can have similar property in Course class. In case of bidirectional mapping you can have utility method like registerCourse in the Teacher class which will set proper relations between entities.
public boolean registerCourse(Course course){
this.course = course;
course.setTeacher(this);
}
You can have this kind of utility method in Course class as well.

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 3.2 Mass Assignment

I'm hoping I can get some help with a mass assignment issue in my recently upgraded Rails 3.2 app.
I understand that in Rails 3.2 attributes are locked down by default and in order for them to be assigned I need to "unlock" them using attr_accessible. This works fine for normal model attributes.
However, I have a homegrown custom property mixin that allows me to add arbitrarily named properties to any model. These properties are stored in the custom_properties table. This mixin leverages method missing to look for a property from that table if I ask a model for a property like: foo.property_foobar.
Each model that uses this mixin can have X custom properties with arbitrary names. I don't have the ability to dictate the names of these properties which obviously makes it difficult to add to attr_accessible.
Does anyone have a recommendation on how I can allow mass assignment of these dynamic properties? I would rather not whitelist all model attributes.
Hopefully all of this makes sense. Thanks everyone!
Louis
One solution is to use attr_protected instead. This allows you to blacklist some attributes while allowing the rest. However, this is a bit harder to secure.
Another solution is to move assignment protection to the controller and allow/reject attributes as needed in each controller/action. There is a gem called strong parameters that allows this and it will also be included in Rails 4. I suggest this solution.
If none of the above work for you, maybe you should try another approach to implement those arbitrary attributes? For example, you could instead store them as a serialized hash in a database column.