Interlinking 2 scaffolds - ruby-on-rails

I am having a problem with some understanding of inter-connection with 2 scaffolds.
I have made one scaffold and used an external xml file to parse the data (nokogiri) and then i have placed that data in a scaffold to auto generate each page for the different items i have (there are only about 50 items).
I have another file with things that are meant to go inside each of those files but they are all stored in 1 xml file. But they are alphabetized so the top one will always remain the top and the bottom one will always remain at the bottom.
Parsing the data will be fine but how would the inter connection go??

You can use the model element to interlinks scaffolds together. You would need a primary and foreign key to be able to conduct that.
has_many :screenings
has_many :films, :through => :screenings
has_one :location

This is a very abstract question (without any models and such). So a very abstract reply.
First you import the 50 items into your scaffolded model. Those 50 items will get new ids.
When trying to import the related items, you will need to find the correct parent-item. Using the ID will not work anymore. So then I see two options:
in a first pass you will have to change your second XML and make sure it can use another unique field (e.g. a name) to find the parent model again
OR you add a new field to your first model, call that something like original_id and use that to build the relation correctly.
Hope this helps.

Related

Edit models with complex primary key with Active Admin

I'm having a trouble with opening AA edit-page for a model, which has a lot of associations.
What I had it's like 50 selects, opening at once. And this page turns to be deadly slow.
After reading this ActiveAdmin: How to handle large associations I considered to use select2 instead of usual select, but things get even worse.
That was because most of the time Rails spent in generating views, not in querying database. So with fancy select2 it reasonably spends even more time in views.
With that knowledge in mind, I decided to not have select inputs on that page at all. So I'll edit "main" object on that slow page, but connected with has_and_belongs_to_many objects should be edited separately.
But after that decision I've faced with a trouble: how should I edit tables with a complex primary key: not just id, but :person_id and :organization_id.
AA by default generates urls like that: /admin/person_organizations/:id/edit, but I need something like this: /admin/person_organizations/:person_id/:organization_id/edit
Any ideas?
ActiveAdmin should be able to handle custom primary keys by default. Just be sure that you add the definition to your model like this:
class Person < ActiveRecord::Base
self.primary_key = 'person_id'
end
After a while I've decided that I don't even need to have multiple keys here since Rails generates artificial id field for habtm tables. And as my goal was to edit this table, I've finished with standard ways of doing this.

Rails acts_as_soft_deletable with has_and_belongs_to_many

I have a problem with the acts_as_soft_deletable plugin and a has_and_belongs_to_many relationship.
I have a model "Place" which has a couple of Categories (like restaurant, hotel, etc). This means that a table "places_categories" is created in the database, containing two columns "place", and "category".
When I destroy a place, it is placed in the table "deleted_places" by acts_as_soft_deletable. Then I try to restore it and the application crashes because a place cannot exist without categories. The entries in "places_categories" that stored which categories the place belonged to are deleted as the place is deleted.
How can I make sure that "places_categories" does not remove the relations when a place is moved to the "deleted_places" table?
Since there is no option to explicitly preserve those entries - you could do something crazy and stupid and just override the delete_sql option to an empty String or somethings thats not going to fail on the "database-side" like so:
class Place
has_and_belongs_to_many :categories, :delete_sql => "select true"
end
This is untested ! Just an idea.
You can read about all available options here.

Before save action... split input field

I have two models and controllers:
Snippets, and Tags.
There is a belongs_to relationship, with tags belonging to snippets. This works well, I have a text field which creates a new tag associated with the snippet when the user submits a snippet.
I want to be able to have a text field which, when submitted to a function (in the model) would split the text on commas (e.g. split(",")).
However, I'm having a little trouble with it.
My though process was, def a function on the Snippet model that splits the input then loops the array of the split, creating a new Tag associated with the Snippet at save time.
Sounds easy enough, but with the RoR magic it's just not happening for me :)
Can someone shed some light / link to a document or something - would be grand!!
Using rails3 with ruby 1.9.2 - normally PHP dev but giving this ruby a go. Magic.
There is a Railscast that explains how to use a virtual attribute in your model to convert submitted "tag_names" into associated models.
It also shows how to do a has_many :through association (called "Taggings") so that tags can belong to many snippets. In your setup, a tag can only belong to one snippet, so you would likely be creating many duplicate tags, when in fact they're the same tag, just associated to different snippets.
If you prefer reading to screencasts, you can check out the transcribed ASCIIcasts.

Live Search / auto_complete + HABTM = possible?

I am attempting to add in a form field that should allow me to add a record into a join table. The table name contains the ids of the following:
log_id
node_id
So naturally, my models is setup as follows:
class Log
has_and_belongs_to_many :nodes
end
class Node
has_and_belongs_to_many :nodes
end
The objective is that when I create a log, I should be able to associate it with an number of nodes (ergo, servers). And since there is a lot of nodes on hand, it seems to make sense to have a textfield where when you enter a node name, it will pop-up a list of nodes to choose from. However, I am having some difficulty getting that accomplished.
I know how to use the autocomplete plugin, (that had came with Rails), but it seems to only accept a string and not with the id - and apparently not across models. I know how to do an AJAX search (though I am not that familiar with Javascript), but again, getting that ID becomes an issue.
I think that in either case, I may be able to figure how to get that value and put that in - the uncertainty is whether one or the other is the correct approach to getting that value. Which one should I concentrate on? Or is HABTM even appropriate in this?

How to create multiple records at once with ActiveScaffold in ROR

I am wanting to use ActiveScaffold to create assignment records for several students in a single step. The records will all contain identical data, with the exception of the student_id.
I was able to override the default form and replace the dropdown box for selecting the student name with a multi-select box - which is what I want. That change however, was only cosmetic, as the underlying code only grabs the first selected name from that box, and creates a single record.
Can somebody suggest a good way to accomplish this in a way that doesn't require my deciphering and rewriting too much of the underlying ActiveScaffold code?
Update: I still haven't found a good answer to this problem.
I suppose you have defined your multi-select box adding :multiple => true to html parameters of select_tag. Then, in the controller, you need to access the list of names selected, what you can do like this:
params[:students].collect{|student| insert_student(student, params[:assignment_id]) }
With collect applied to an array or enum you can loop through each item of that array, and then do what you need with each student (in the example, to call a function for insert the students). Collect returns an array with the results of doing the code inside.
if your assingnments have has_many :students or has_and_belongs_to_many :students, then you can change the id of the multi-select box to assignment_student_ids[], and it should work.
I was referred to BatchCreate, an ActiveScaffold extension which looks like it might do the trick.

Resources