Issue
I am developing a ruby on rails application which I need to use the existing database developed by grails application. In that grail application they are using spring security oauth2 library.
I came to know that spring library is using hibernate ORM and my rails application is using ActiveRecord ORM. In hibernate has_many association is as follows:
class OAuthClient {
private static final String NO_CLIENT_SECRET = ''
transient springSecurityService
String clientId
String clientSecret
Integer accessTokenValiditySeconds
Integer refreshTokenValiditySeconds
Map<String, Object> additionalInformation
static hasMany = [
authorities: String,
authorizedGrantTypes: String,
resourceIds: String,
scopes: String,
redirectUris: String
]
}
In the above example has_many declaration will create associate tables without any domain models.
But in rails we need to create both models like:
class A < ActiveRecord::Base
has_many :bs,
end
class B< ActiveRecord::Base
belongs_to :a
end
Is there any solution to implement hibernate has_many association in Rails(Without a model)?
Note: I am not using the "B" model for any individual operation.
Thanks in advance.
Solution
In short - no. Thats just not how Rails and ActiveRecord work. In ActiveRecord you have models which each correspond to a database table.
In ActiveRecord relations are between models - thats how AR knows how to join and what to do with the data it pulls from the database. Relations in AR unlike hibernate are not just an array - rather they are scopes which are lazy loaded and AR needs the joined model to enable reflection on the relation.
a = thing.accessories # a lazy loading scope
a = a.where(color: 'Blue') # adds a condition to the scope
a.to_a # fetches from the DB.
Models in general are the most important part of the Rails flavor of MVC so I don't really see any specific benefits in skirting them.
If you want to store a bunch of auxiliary on a record data several RDBMS systems have data types such ARRAY, HSTORE and JSON. However these are treated as a single column as far as AR is concerned and not a has_many
relation.
However in this case you can use the legacy database - but that does not mean that your object relational mapping needs to match Hibernate. It just means that you need to do a lot more configuration in telling ActiveRecord how you models map to the schema and foreign keys etc.
However unless you need rails to share the database with the legacy app its usually a lot more sane to migrate the database so that it follows the rails conventions.
If your applications are only sharing the authentication it may make more sense to setup a OAuth authentication provider which both apps utilize.
Answered By - max Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.