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
10
votes
2 answers

Mongoid: How to load only some fields of an object you lazy load via reference?

For performance reason, I use as often as possible the only() keyword when writing up a mongoid query in order to specify the fields I want to load. The usual suspect, is for instance when I want a user's email of all my admins only for display…
Alex
  • 4,367
  • 5
  • 29
  • 45
9
votes
2 answers

Rails - How to manage nested attributes without using accepts_nested_attributes_for?

My problem is I've run into limitations of accepts_nested_attributes_for, so I need to figure out how to replicate that functionality on my own in order to have more flexibility. (See below for exactly what's hanging me up.) So my question is: What…
8
votes
1 answer

has_many :through association cannot find a valid model

I am creating association pretty much identical with the Rails Guides Patient-Appointment-Physician data model. A user has many prospects through prospect_subscription. However, when trying to access user.prospects in rails console, it throws the…
8
votes
2 answers

Multiple Associations to the Same Model in CakePHP 3

I am using cakePHP version 3.x. When i query the MessagesTable i want to get the Users data for the the sender and the receiver of the message. I have done this many times in cakePHP 2 but i cant figure out why it isn't working in version 3.x. i…
Wisd0m
  • 521
  • 1
  • 5
  • 14
7
votes
5 answers

cakePHP - how do i get the items count for a related model?

The models are: stores and product, and they are associated by: var $belongsTo = array( 'Store' => array( 'className' => 'Store', 'foreignKey' => 'store_id' )) var $hasMany = array( 'Product' => array( 'className' =>…
yossi
  • 3,090
  • 7
  • 45
  • 65
7
votes
3 answers

CakePHP Fatal Error Call to a member function schema() on a non-object

I have some trouble finding a solution for this.. Error: Call to a member function schema() on a non-object File: /Cake/Model/Model.php Line: 3627 In my Database there are the tables articles,hashtags and the association articles_hashtags with the…
6
votes
1 answer

Please explain the has_many, through: source: Rails Association

I've found a bunch of articles, stackoverflow answers and rails documentation about 'source:', but none of it explains this association in a way I can understand it. I need the most simplified explanation of this way of associating, if possible. My…
6
votes
1 answer

rails build or update method?

I'm currently using the find or create method to update an associated record which i use to store cached information, but I'm wondering if there's some simpler alternative method similar to build since the object is a has_one relation. The problem…
holden
  • 13,471
  • 22
  • 98
  • 160
6
votes
2 answers

CakePHP 3 : Association property name clashes with field of same name of table

I am using CakePHP 3.2. I have two tables service_requests and coupon_history service_requests table CREATE TABLE `service_requests` ( `id` char(36) NOT NULL, `request_id` bigint(20) NOT NULL, `user_id` char(36) NOT NULL, `user_address_id`…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
6
votes
2 answers

Cakephp 3.0 Save associated model

I am learning cakePHP 3.0 and have some problem with saving associated data on my model. I try to save a Client with associated data of ClientPreferences ClientTable class ClientsTable extends Table { public function initialize(array $config) …
6
votes
2 answers

Porting complicated has_many relationships to Rails >4.1 (without finder_sql)

I am porting a Rails app to Rails 4.2. This Rails app contains some rather complex manual SQL code in associations - partly due to DB optimizations (e.g. subselects instead of JOINs), partly due to no feasible alternative at the time of writing…
Jens
  • 1,386
  • 14
  • 31
6
votes
1 answer

Using rails_admin to display dropdown list on belongs_to association

I am using rails_admin to manage data in a rails application. I have this class: class Activity < ActiveRecord::Base attr_accessible :content, :title, :category_id belongs_to :category, :inverse_of => :activities end And the other end is: class…
Apollo
  • 1,913
  • 2
  • 19
  • 26
6
votes
3 answers

Multiple belongs_to to the same table

I have two tables: currencies and rates currencies: id:int, code:string, name: string rates: id:int, top_currency_id:int, bottom_currency_id:int, rate:float And I have two active records for them: class Rate < ActiveRecord::Base attr_accessible…
Grisha
  • 77
  • 1
  • 1
  • 7
6
votes
2 answers

Rails - how to order a model by its has many association?

I have two models: #Product class Product < ActiveRecord::Base has_and_belongs_to_many :categories attr_accessible :name ... end #Category class Category < ActiveRecord::Base has_and_belongs_to_many :products, :order => "name ASC" …
Kulgar
  • 1,855
  • 19
  • 26
6
votes
2 answers

Entity Framework Cardinality Issue on a 0...1 Association

I have database tables that look like this: A Task can be mapped to a Module, or not mapped at all (0...1). I'm using Entity Framework database-first, and when I generated the model from the database, the Task entity came through with Modules as a…
AJ.
  • 16,368
  • 20
  • 95
  • 150
1
2
3
57 58