Duplication of models with multiple nested & non-nested belongs_to relationships - ruby-on-rails

Apologies if the title isn't accurate, I had a difficult time distilling this question into a single line. I'm using the amoeba gem and trying to figure out if it's possible to associate a duplicated model with multiple belongs_to relationships.
For instance:
class Building < ActiveRecord::Base
has_many :floors
has_many :layout_groups
end
class Floor < ActiveRecord::Base
belongs_to :building
has_many :units
end
class Unit < ActiveRecord::Base
belongs_to :floor
has_many :layouts
end
class Layout < ActiveRecord::Base
belongs_to :unit
belongs_to :layout_group
end
class LayoutGroup < ActiveRecord::Base
belongs_to :building
has_many :layouts
end
As you can see, Layouts belongs to both Unit and LayoutGroup. If I'm making a complete duplicate of Building, how do I associate layouts with both units and layout_groups but also ensure that two sets of layouts are not created in the process?
Im open to using a non-amoeba solution, I just stated there as it was suggested.

Related

Select attributes from multiple tables with no association

If could really help me out with problem I have, I would really appreciate it. My problem is that I can not select attributes from certain tables that have no association between them.
To explain myself better, here are my models:
class User < ActiveRecord::Base
has_many :measurement_blocks
has_many :measurements, through: :measurement_blocks
end
class MeasurementBlock < ActiveRecord::Base
belongs_to :user
has_many :measurements
end
class Measurement < ActiveRecord::Base
belongs_to :measurement_block
end
class Device < ActiveRecord::Base
has_many :measures
end
class Measure < ActiveRecord::Base
belongs_to :device
end
I can get a ProxyAssociation with
user.measurements
but what I really want is to include the device and measures names.
I have tried the following:
measurements = user.measurements
measurements.include("INNER JOIN ON devices (devices.id = measurements.device_id)")
but it does not work. Furthermore, as I mentioned before I want to
include the measures and devices names along with the measurements.
ActiveRecord is frustrating to me when there is no full association between models.
Thank you again.

Rails relationships setup for 5 table db

I'm stuck in a circle of indecision and need a shove to break free. I'm a noob with Rails and I'm attempting to translate a pre-existing database using Rails conventions. Basically, I currently have 5 models/tables: Workorders, Mileage_logs, Time_logs, Parts, & Equipment. A Workorder can have many Mileage_logs, Time_logs, and Parts, because each of them is shown on the Workorder's index page. But, that's the seemingly easy part...
I'm getting confused when the Equipment model is introduced because it seems like it's basically the same thing as the Workorder.
What is the best way to handle this relationship setup? Is this an appropriate use for the has_many :through convention? Or, is this best done with simply having the workorder_id and equipment_id in the Mileage_log, Time_log, and Part models and then:
class Part < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Mileage_log < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Time_log < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Workorder < ActiveRecord::Base
has_many :Time_logs
has_many :Parts
has_many :Mileage_logs
end
class Equipment < ActiveRecord::Base
has_many :Time_logs
has_many :Parts
has_many :Mileage_logs
end
Or, is the has_many through: relationship what I should look into for the Workorder & Equipment models?
class Workorder < ActiveRecord::Base
has_many :parts
has_many :mileage_logs
has_many :time_logs
end
class Equipment < ActiveRecord::Base
has_many :parts, through: :workorder
has_many :mileage_logs, through: :workorder
has_many :time_logs, through: :workorder
has_many :workorders
end
Any help would be greatly appreciated!
Also, any general advice on the route setup would be welcomed as well.

How to setup a association through multiple models

I've the followig three models in Rails:
class Book < ActiveRecord::Base
has_many :chapters
has_many :pages
end
class Chapter < ActiveRecord::Base
belongs_to :book
has_many :pages
end
class Page < ActiveRecord::Base
belongs_to :chapter
end
How is it possible do to a query like: Book.first.pages.count to get the number of pages of the whole book. By now I dont even know if i set my model up the right way. Would be great if you could help me out here.
Thanks in advance!
You can use a has_many through relationship as outlined here
class Book < ActiveRecord::Base
has_many :chapters
has_many :pages, through: :chapters
end
class Chapter < ActiveRecord::Base
belongs_to :book
has_many :pages
end
class Page < ActiveRecord::Base
belongs_to :chapter
end
That enables you to do book.pages.count
The issue may lie in your data model. The associations as listed seem a little odd - a Book has many Chapters and many Pages, but a Chapter can also have many Pages. How do you differentiate between the Pages in a Book compared to a Chapter?

Rails: accessing associated models with several degrees of separation

I am a beginner in rails, and I have a question about accessing one model from another model that is associated with several degrees of separation.
Let's say I have these models:
Account has_many Spies
Spy has many SpyRelationships and belongs to Account
SpyRelationship belongs to Listing and belongs to Spy
How would I set up the associations so that I could simply pull all the listings associated with a given Account (via its spies and spyrelationships)? What line of code would allow me to do so, after those associations are setup properly?
I'm guessing you want to access a listing through a spy?
class Account < ActiveRecord::Base
has_many :spies
has_many :listings, through: :spies
end
class Spy < ActiveRecord::Base
belongs_to :account
has_many :spy_relationships
has_many :listings, through: :spy_relationships
end
class SpyRelationship < ActiveRecord::Base
belongs_to :listing
belongs_to :spy
end
class Listing < ActiveRecord::Base
has_many :spy_relationships
end

Setting up a Has_Many :through Association

On the application I'm currently working on, I'm stuck on setting up the associations between 3 models to ensure referential integrity. I have an Event model, Building model, and a Room model. The association in real life is pretty intuitive. An Event can only be in one Building and one Room. A Building clearly can have multiple rooms.
Here's what I have set up now. However, how can Events specify their room if they belong to Buildings, and the foreign key for the Room is in the Events table? Is this where you use a has_many :through relationship? Is it good practice to store both the Building and Room foreign keys in the Event table, as Rooms are owned by Buildings? What about the conditional relationship that requires a building to be specified before allowing a room to be specified (some buildings have 2 rooms, others have 20, for example)
Sorry I'm so unclear on this. Thanks in advance for the help!
class Event < ActiveRecord::Base
belongs_to :building
end
class Building < ActiveRecord::Base
has_many :events
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :building
end
I think the best way to handle this is to do something like the following:
class Event < ActiveRecord::Base
belongs_to :room
has_one :building, :through => :room
end
class Building < ActiveRecord::Base
has_many :events
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :building
end
So you can use has_one :through to specify that an event owns a hotel
I would recommend the following:
class Event < ActiveRecord::Base
belongs_to :room
has_one :building, through: :room
end
class Building < ActiveRecord::Base
has_many :events, through: :rooms
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :building
has_many :events
end
This way you can do #room.events, #event.building, #building.events

Resources