How to convert an attribute to a reference in rails - ruby-on-rails

I am new to ruby on rails and I am working on an existing project in which I think some of the attributes of the models should be changed to be independent models; then they should referenced in the original models. What are the needed tasks to do this? Should I first remove those columns and then generate new models based on the attributes or there is easier way?
Thanks in advance.

There is no way to automatically spin off attributes into a separate model definition.
If there's existing data that you want to preserve, then create the new model with a migration and with a foreign key to the original model, and then write code to transfer the data.
Primary.each do |p|
p.secondary = Secondary.new
p.secondary.attribute_1 = p.attribute_1
p.secondary.attribute_2 = p.attribute_2
p.secondary.save
end
Then delete the primary attribute_1 and attribute_2 columns
If there is no existing data you need to preserve, then just create the new model and delete the old attributes using migrations.

Related

Move a Core Data entity and its data into new Core Data model file through Migration

Is it possible to move a Core Data entity Car and its data in Model1 into a new Core Data model Model2 using migration? Model2 will also have a new CarOwner entity + other new relationships (so model file is different than Model1). Is this possible using lightweight migration or do I have to use custom migration? I'm using Magical Record to setup my Core Data Stack.
I have been using lightweight migration for years with success. So I don't have any custom migration mechanism in place. I want to ask first before I implement a new system so I can incorporate CD custom migration into my existing MagicalRecord Core Data stack.
EDIT: updated question to clarify that Model1 and Model2 have differences.
With the same xcdatamodel file and different versions you can use a Mapping Model file, but being different mom files i guess those are different stacks and migration will not work.
Model migration is only relevant if the model has changed-- meaning, if the entities contained in the data model don't match the entities saved in the persistent store file. Migration doesn't depend on what model file you use, it depends on the entity hashes contained in that model.
Meaning: If your new model file has exactly the same entities as the persistent store file has, you don't actually need to migrate anything. Just start using the new model file. However keep in mind that if you ditch the old model file and all of its old versions, you won't be able to migrate from older versions of that model any more.

In Rails should i generate a model to contain simple plain list?

I'm currently building a web-page with Ruby on Rails. Im creating A data model that works as follows: Project has many Milestones and milestone has many goals
Example:
Project.milestones.goals
So I generated a model for Project and a model for milestones but generating a model for list of goals seems like big and unnecessary.
Should I generate a model for goals or is there something else that would fit more for this purpose?
It all comes down to this question: Goals need to be persisted on your database?
If the answer is yes, then you must create a model that inherits from ActiveRecord::Base, and if the answer is no, you could create a model, that don't have the need for persistence methods of ActiveRecord, but acts like an Enum, having static values and stuff.

Rails 3.2, what's the best way to associate/lookup to a legacy table belonging to another schema?

New Rails guy here...
I have a SimpleForm model which belongs to a Parts table belonging to another schema that's not Rails application.
What's the best way to model this association so that my form can do a lookup of the Parts table for part_id and the model can validate the part_id foreign key against the legacy Parts table?
possibilities:
create a database view and activerecord model in rails app?
create a readonly model with query/connection to Parts table?
Please be specific, as I really don't know much within Rails.
I am using Rails with Oracle and Windows, so any solution has to work with these.
Thanks in advance!
I ended up using database migration to create a database view and created readonly model for it.
In my SimpleForm, setup belongs_to relationship to the new Parts model and validates_presence_of to enforce the foreign constraint on inserts and edits.
Looks like this is pretty good way to do readonly foreign key reference by granting only "select" permission to legacy table and also setting Rails model to readonly.

How to save 2 id in joint table for many-to-many relationship in rails 3.1

There are two models. One is rfq and another one is standard. It is like:
class Rfq << ActiveRecord::Base
has_and_belongs_to_many :standards
end
class Standard << ActiveRecord::Base
has_and_belongs_to_many :rfqs
end
Table rfqs_standards has been created. My question is when creating rfq, how to save the paid of rfq_id and standard_id in table rfqs_standards automatically.
Thought adding accepts_nested_attributes_for :standard in rfq model. However since there is no real attributes (but just pair of id) saved for this many-to-many relationship, this seems not the right way.
Both rfq and standard was declared in routes.rb as resources :rfqs and resources :standards.
The procedure is when creating rfq, standard will be picked up via a drop down list. Then rfq is saved and at the same time, a new entry in joint table is created. When creating new standard, there is no need to create entry in joint table.
Any suggestion to save the id in joint table? Thanks.
this is easier than you might think because it's handled automatically by ActiveRecord.
When you say "has_and_belongs_to_many", you're telling AR to associate those two models with a many-to-many relationship using the table, and for the most part you no longer need to worry about the join table. When you add an instance of Standard to an Rfq's list of standards, this will be done for you.
Here's an example:
rfq = Rfq.create
standard = Standard.create
rfq.standards << standard
We've created each of the objects, and the third line creates the connection, saving a new record in the rfqs_standards table with the proper ids. rqf.standards looks and acts like a normal array, but when you assign objects to it, ActiveRecord does the database work for you.
After creating the records, you could have also done:
standard.rfqs << rfq
You could also do both at the same time:
rfq = Rfq.create
standard rfq.standards.create
This created an rfq, then created a standard that is automatically connected to the rfq. You can do the same thing in reverse:
standard = Standard.create
rfq = standard.rfqs.create
I hope this helps!
UPDATE: Since you mentioned forms and automatic saving, read my article on nested attributes that shows how to implement that, including full code samples.

Rails Generate Model from Existing Table?

I'm very new to the rails framework and want to know how to generate a model based on an existing table. For example, I have a table named person and want to generate the model based on the columns from that table. However, whenever I use "ruby script/generate model Person --skip-migration it creates an empty table named people and creates the model after that. Is there a way to generate a model after a table named person?
Thanks.
Rails is very opinionated, so if you have a table called "person" and you want the corresponding model to be called Person, you need to tell Rails explicitly not to be so clever (otherwise, it will assume that it needs to look for the plural of the model name for the table name).
class Person < ActiveRecord::Base
set_table_name 'person'
end
If your table's primary key isn't called "id", then you'll need to specify that, too...
set_primary_key 'person_id'
You may also need to specify a different autoincrement sequence name, depending on your database.
There's not a way that I know of to automatically generate a model from an existing legacy table, but this should get you most of the way there.

Resources