Importing csv file to nested resource models (has_many :through etc.) - ruby-on-rails

I hope that someone can help me with importing csv file into nested resource model.
I have a couple of models (Rails 4.0, Ruby 2.0):
class Level < ActiveRecord::Base
belongs_to :level_type
has_many :project_levels
has_many :projects, through: :project_levels
end
class LevelType < ActiveRecord::Base
has_many :levels
end
class ProjectLevel < ActiveRecord::Base
belongs_to :level
belongs_to :project
end
class Project < ActiveRecord::Base
belongs_to :company
has_many :project_levels
has_many :levels, through: :project_levels
end
class Company < ActiveRecord::Base
has_many :projects
end
And what I do similar to this in a rake task:
company.projects.create(company_name: row[3])
level_type.levels.create(position: row[4])
My problems is how to create levels from companies? Is this the right thing to do?
company.projects.levels.create(company_name: row[3])
What is confusing me is that I have 2 sides to get to the levels and the same information I have to put into it (company_name: row[3]) and I am not sure how to deal with it. Any help would be appreciated.

Related

Structuring associations Rails 4

This is probably a fairly straight forward answer that I feel like I should know, but occasionally I run across something like this that stumps me.
I'm working on a rails app that requires me to essentially create a leasing system for rentals.
I've got a user, a building, a lease and an unit. The way I've got it structured right now is:
class Buildings < ActiveRecord::Base
has_many :units
has_many :users
end
class User < ActiveRecord::Base
belongs_to :buildings
has_many :units, through :lease
end
class Lease < ActiveRecord::Base
belongs_to :user
belongs_to :unit
end
class Unit < ActiveRecord::Base
belongs_to :building
belongs_to :user
has_one :lease
end
I'm running into syntax errors and association errors and the documentation is as clear as mud. Perhaps someone can help me to properly structure these associations.
Your syntax error is on the User class
Change
has_many :units, through :lease
to
has_many :unit, through: :lease
or
has_many :units, :through => :lease

Specifying Conditions on Multiple Nested Eager Loaded Associations

I'm trying to query on ActiveRecord multiple nested eager loaded associations with conditions like so:
user.books.includes(slot: [room: :school]).where("books.slot.room.school.id = 1")
Obviously this query is wrong, but basically what I'm trying to reach is a relation of user.books for a certain school.
My model structure is:
class User < ActiveRecord::Base
has_many :books
has_many :slots, through: :books
has_many :rooms, through: :slots
has_many :schools
end
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :slot
end
class Slot < ActiveRecord::Base
has_many :books
belongs_to :room
end
class Room < ActiveRecord::Base
belongs_to :school
has_many :slots
end
class School < ActiveRecord::Base
has_many :users
has_many :rooms
has_many :slots, through: :rooms
has_many :books, through: :slots
end
Any ideas? Thanks in advance.
includes eager loads association records, while joins does what you want to do.
This question has been asked here numerous times. Please make sure that you look and try to find a similar question here before you ask one.
So you want to change your code like this:
user.books.joins(slot: [room: :school]).where(schools: { id: 1 })

Rails activerecord deep eager loading

Here is my models:
class User < ActiveRecord::Base
has_many :products
has_many :comments
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
I need to get comment records from current user products only
How do I do that? thanks
If we move the relationships to use a has_many: comments, through: products you can probably get what you're after:
class User < ActiveRecord::Base
has_many :products
has_many :comments, through: products
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
Now you can do user.comments.
The rails docs are here, which say:
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model. For example, consider a
medical practice where patients make appointments to see physicians.

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.

Rails 4, nested association search

I have the following associations.
class Farm < ActiveRecord::Base
has_many :crops
end
class Crop < ActiveRecord::Base
belongs_to :farm
has_many :seed_batches
end
class SeedBatch < ActiveRecord::Base
belongs_to :crop
has_many :tasks, through: :task_batches
end
class Task < ActiveRecord::Base
has_many :seed_batches, through: :task_batches
end
class TaskBatch < ActiveRecord::Base
belongs_to :task
belongs_to :seed_batch
end
In essence, a farm has many crops. each crop has many seed batches. each seed batch has many tasks.
My question is this: How can i get all the tasks knowing the id of a farm?
I have tried many ways to do .where() searches, but all came up to errors. Can anyone please enlighten me?
Try
Farm.find(1).crops.each(&:seed_batches).collect(&:tasks)
You should be able to define has_many :tasks for Crop and Farm:
class Farm < ActiveRecord::Base
has_many :crops
has_many :tasks, through: :crops
end
class Crop < ActiveRecord::Base
belongs_to :farm
has_many :seed_batches
has_many :tasks, through: :seed_batches
end
Then you should be able to access all tasks with Farm.find(id).tasks

Resources