Questions tagged [model-associations]

Models can associate in three main ways: one to many, one to one, and many to many.

This is tagged primarily in Ruby community. Models can associate in three main ways: one to many, one to one, and many to many.

865 questions
5
votes
3 answers

CakePHP 3.0 Can't save hasMany associated data

I've problem when saving data with hasMany association This is my table 1) post table: each item has an unique id. id | title | ... 1 | Aloha | ... 2) images table id | post_id | image | ... 1 | 1 | abc.jpg | ... 2 | 1 | efg.jpg…
rslhdyt
  • 149
  • 2
  • 14
5
votes
2 answers

Rails 3 can't perform validation for persited object when use collection_singular_ids=ids method

Is there any way to avoid automatically saving object while assigning collection attributes(collection_singular_ids=ids method)? for example, I have the following Test and Package model, Package has many tests. User can build package bundle with…
5
votes
1 answer

Data Modeling 3 Way Table has_many association

I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy …
5
votes
1 answer

Undefined method for nil class in Rspec test

So, I have a Game class that can have many Versions, and each Version can have many GameStats. I have my Each Version belongs_to a Game, and each GameStat belongs_to a Version and has_one Game through the Versions. In my tests, when I test for…
5
votes
2 answers

Source Reflection Errors with has_many :through

I'm attempting to create a system where my site's users can favorites pages. Those pages have two types, either clubs or sports. So, I have four models, associated as such: User Model: class User < ActiveRecord::Base .. has_many…
5
votes
1 answer

Plural Name in Association in Rails

I have user model and a car model I want to have a model which will hold the settings for each car and each user so I do class CarSettings < ActiveRecord::Base belongs_to :user belongs_to :car end for user: has_many :car_settings and for…
4
votes
1 answer

Efficient Join queries - Can it be done using ActiveRecord

I have 4 models, A, B, C and D class A < ActiveRecord::Base has_many :B has_many :C, :through => :B end class B < ActiveRecord::Base belongs_to :A has_many :C has_many :D, :through => :C end class C < ActiveRecord::Base …
Atarang
  • 422
  • 1
  • 6
  • 22
4
votes
2 answers

How can I check if one ActiveRecord class belongs to another

Given two subclasses of ActiveRecord::Base, how can I implement a function that checks to see if one belongs to the other? def ClazzA < ActiveRecord::Base belongs_to :clazz_b end def ClazzB < ActiveRecord::Base has_many :clazz_a end def…
maxenglander
  • 3,991
  • 4
  • 30
  • 40
4
votes
1 answer

Multiple entries in a :has_many through association

I need some help with a rails development that I'm working on, using rails 3. This app was given to me a few months ago just after it's inception and I have since become rather fond of Ruby. I have a set of Projects that can have resources assigned…
4
votes
1 answer

Rails Rich Association - Overwriting to association model

Suppose we have three models PATIENTS, NURSES, and their association model APPOINTMENTS. This week all patients have to meet with their nurses. A patient has many nurses, and a nurse has many patients. Likewise, a patient has many appointments, but…
Ian
  • 369
  • 1
  • 2
  • 11
4
votes
3 answers

Rails associtating three models

I have the following models in my rails application class User < ApplicationRecord end class Group < ApplicationRecord end class Role < ApplicationRecord end Every Group has_many User with different Role. For example Group-1 have User - A with…
4
votes
2 answers

Composition is not "Composition"

Composition: A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship. By Deitel P.J., Deitel H.M. - Java How to Program 9th Edition. This viewpoint is…
atorres
  • 402
  • 4
  • 11
4
votes
1 answer

Rails Polymorphic associations with name space

I want to save different results(default and manual), each result can have a reason. Thought that this would be a good place for a polymorphic association. The Models are namespaced however and this is prooving to be tricker than anticipated.…
4
votes
1 answer

Rails: Association between Category, Subcategory, and Lawyer

I have a vast list of Lawyers, Categories, and Subcategories. Hint (so you could have a clue if my associations are okay) On Categories Table, I do not want to see a column on Categories Table referencing Subcategories. On Subcategories Table, I…
4
votes
1 answer

List all associations as links in active admin

Is it possible in active admin to list all of the has_many associations of my record as links? The code should look something like: column "belongs to" do |b| b.associations.map { |a| link_to(a.name, admin_association_path(a) } end But this…
azrosen92
  • 8,357
  • 4
  • 26
  • 45
1 2
3
57 58