multiple belongs_to for a model - activerecord

I have this model "Comment" which is given by a model "User" for a given "city" and "department".
While creating the schema for table "comments", I put in columns city_id, department_id and user_id which should act as foreign keys to respective ids in tables cities, departments and users.
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :city
belongs_to :department
end
Cities and Departments are independent tables which are populated with reference data (which would be used to populate in the forms.
When I try to access comment.city.name, I get a "undefined method `name' for nil:NilClass".
Table cities is defined with columns -"id", "name" and "symbol".
What is the root cause of this error?
What else do I need to do ? I have tried even by putting has_many :feedbacks in class City and class Department (even though it should not happen because they are independent of comments). I am missing something basic here, it seems.
Thanks,
Ashish

I think you need a has_many to go with every belongs_to. So each of your classes that comments belong_to (User, City, Department) should have
has_many :comments

Related

Activerecord/Datamapper - Have one child belong to many parents

How would you set up an activerecord/datamapper association for the following scenario:
A user creates a "bookshelf" which has many books(a book object just has an isbn that is used to query an api, and has_many review objects associated with it). Let's say Jack creates a "bookshelf" with a book object. Then, lets say that Jill creates a "bookshelf" with the same book object(it has the same id and the same reviews). The book object has the following code as of now:
class Book < ActiveRecord::Base
has_many :reviews
end
Then, when you view the page for a book (you click the link to it from the "bookshelf" created by Jack) you should see the same book object when you clicked the link to it from Jill's "bookshelf" (e.g. both "bookshelves" have a link to /books/23 because they have the same book object).
I have not been able to figure this out with the has_many association because that requires me to make a new book each time a user adds a book to their "bookshelf." I have trouble understanding the has_and_belongs_to_many relationship, is that what should be used here? I was not able to find any similar questions on SO, so any help is greatly appreciated.
I am using Rails 4 with Ruby 2.1.
Here is a drawing of what I would like to accomplish:
Drawing
Yes, you would have to define many-to-many relationship between a Bookshelf and a Book. There are two ways to achieve this in Rails:
Option 1) Use has_and_belongs_to_many
See guide
According to official documentation has_and_belongs_to_many association:
Specifies a many-to-many relationship with another class. This associates two classes via an intermediate join table. Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names. So a join between Developer and Project will give the default join table name of “developers_projects” because “D” precedes “P” alphabetically.
So, your classes should look like this:
class Bookshelf < ActiveRecord::Base
has_and_belongs_to_many :books
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :bookshelves
has_many :reviews
end
Add a join table generation to your migrations:
class CreateBooksBookshelvesJoinTable < ActiveRecord::Migration
def change
create_table :books_bookshelves, id: false do |t|
t.belongs_to :book, index: true
t.belongs_to :bookshelf, index: true
end
end
end
This will create a books_bookshelves table in your database. The table will have no primary key. There would be two foreign keys to your models Book and Bookshelf.
So, if you call self.books in the context of an user's bookshelf, you will get a list of books in the bookshelf. Vice versa, calling self.bookshelves in the context of a book will return a set of bookshelves the book belongs to.
The problem with this approach is that every time you add a new book to the bookshelf a new record is created in the database. If you are okay with that, there is no easier option than using has_and_belongs_to_many association. Otherwise, I recommend you to go with the Option #2.
Option 2) Use has_many :through
Another option is to use has_many, :through association (see guide). You would have to define one more model to do that, but it might come handy in some use cases (see below for an example).
Your classes should look like this:
class Bookshelf < ActiveRecord::Base
has_many :books, through: :books_bookshelves
has_many :books_bookshelves
end
class Book < ActiveRecord::Base
has_many :bookshelves, through: :books_bookshelves
has_many :books_bookshelves
has_many :reviews
end
class BooksBookshelf < ActiveRecord::Base
belongs_to :book
belongs_to :bookshelf
end
Probably the best thing about using has_many :through association is that it allows you to add custom columns to the join table (e.g. add column count to keep track how many books of the same type are there in the bookshelf).
The migration would look pretty much the same as the one we used in Option 1, except for the fact we are adding an unique constraint on the foreign keys (please note that adding the constraint is optional):
class CreateBooksBookshelvesJoinTable < ActiveRecord::Migration
def change
create_table :books_bookshelves, id: false do |t|
t.belongs_to :book, index: true
t.belongs_to :bookshelf, index: true
# add your custom columns here
end
add_index :books_bookshelves, [:book_id, :bookshelf_id], unique: true # to make sure you won't create duplicate records
end
end
By going with this approach, adding a new would be a bit more complicated as you would have to make sure you are not inserting duplicate records in the join table. (However, you may remove the unique constraint from the migration, to achieve exactly the same kind of behavior as you would get with has_and_belongs_to_many.)

Rails association, I want something but the other model shouldn't care

So I think I need help with what I am doing with my asscoiations. I have two models (well many but for the purposes here). One is a Provider. Each provider can have a designation. Though the Designations is just a list it can appear in many different providers so it is not one to one. My models are as follows:
Provider.rb
class Provider < ActiveRecord::Base
has_and_belongs_to_many :groups
has_one :designation
has_one :specialty
end
Designation.rb
class Designation < ActiveRecord::Base
end
The error I get is this:
SQLite3::SQLException: no such column: designations.provider_id: SELECT "designations".* FROM "designations" WHERE "designations"."provider_id" = ? LIMIT 1
Which tells me my associations are off cause the designations should not have the provider_id, if anything the provider should have the designation_id.
Do I ditch the has_one and just put has_many in the Designation?
If I do that, I am new to rails, do I have to create a migration so the database is correctly updated?
Try using
models/provider.rb
belongs_to :designation
Then in
models/designation.rb
has_many :providers
It may feel a little strange but the belongs_to just lets you know which model the id column needs to go in. In this case the provider model, so you'll need:
rails g migration add_designation_id_to_provider designation_id:integer

Is there a difference between has_one on one model vs belongs_to on the other?

I understand that a belongs_to puts the foreign key on the declaring model, and a has_one puts it on the other model. Does that mean there is no difference in this example?
class Category
belongs_to :blog
end
class Blog
end
class Blog
has_one :category
end
class Category
end
The only thing I can see is that the second example's nomenclature makes a little more sense.
Yes.
belongs_to expects the foreign key to be on its table whereas has_one expects it to be on the other:
# here the Category table will need to have a blog_id field
class Category
belongs_to :blog
end
# the blog table won't need anything
class Blog
has_one :category
end
has_one under the hood is similar to has_many except it adds a LIMIT 1 clause to the sql statement when you query the table.
The difference lies in the database, as you've noted. The model with the belongs_to reference must contain the foreign key for the association. When using has_one, it will expect to find a foreign key on the associated model.

How to reference values in a join table for a rails where method?

This is a noob level question.
i've got two models: Patient and Provider joined through a table Chart.
i used the association "has_many :through" rather than "has_and_belongs_to_many" because i need to have another column added to the Chart table [called patient_mrn] which i understand that i cannot do with the "has_and_belongs_to_many" scenario.
the Patient model has:
has_many :charts
has_many :providers, :through => :charts
the Provider model has:
has_many :charts
has_many :patients, :through => :charts
and the Chart model has:
belongs_to :patient
belongs_to :provider
i am trying to call a where method on the Patient model to retrieve all patients with the conditions that:
-the provider_id in the Chart join table for that patient equals a given value [#exam.provider_id] and
-the patient_mrn in the Chart join table for that patient equals a given value[#exam.patient_mrn].
this is what i came up with to try but it clearly isn't working. Where am i going astray?
#patient = Patient.where(:patient.chart[provider_id] => #exam.provider_id,
:patient.chart[patient_mrn] => #exam.patient_mrn)
Joining tables is what you need to do in order to specify conditions on a model's associated table. (Look at the section on specifying conditions for an example of what you're looking to do).
But, in short, you want to join to your chart table and specify conditions on that. Your query should probably look something like:
#patients = Patient.joins(:charts).where(:charts => { :provider_id => #exam.provider_id, :patient_mrn => #exam.patient_mrn })
This should return all Patients whose chart has the given provider_id and patient_mrn.

Multiple has_one association to one polymorphic model

I have two models, Address and User:
class Address < ActiveRecord::Base
belongs_to :resource, polymorphic: true
end
class User < ActiveRecord::Base
has_one :contact_address, class_name: "Address", as: :resource
has_one :billing_address, class_name: "Address", as: :resource
end
Problem is if I create billing_address for User it will be automatically set as contact_address, since the addresses table doesn't specify a different resource_type (both are User).
Can you give me some advice on how I should set up my models?
Thanks
I would say a billing address is more to do with an order than a person. For example, on one order I might want you to bill me at work, another at home.
Also, a person can have many addresses, but an address can also have many people. It is a network, not a hierarchy. The classic representation:
PARTY
id
type
name
PARTY_ADDRESS
id
party_id
address_id
type {home, work}
ADDRESS
id
suite
...
ORDER
id
date
customer_party_address_id
bill_to_party_address_id
ORDER_ITEM
id
order_id
product_id
price
quantity
ship_to_party_address_id
I choose easy way, added another foreign_key for addresses table. After that:
has_one :contact_address, class_name: "Address", as: :resource, foreign_key: :foreign_key
Single table inheritance can help you out. To get this to work you have to add a type column to the locations table through a migration (as you do with all Single Table Inheritance models).
class Address < ActiveRecord::Base
belongs_to :resource, polymorphic: true
end
class ContactAddress < Address
end
class BillingAddress < Address
end
class User < ActiveRecord::Base
has_one :contact_address, as: :resource
has_one :billing_address, as: :resource
end
For what it's worth, the belongs_to relationship name (:resource) is a bit weird. Maybe :addressable would make more sense?

Resources