associations

Eager load associations with Active Model Serializers

泪湿孤枕 提交于 2019-12-18 11:27:17
问题 Background I have a rails application with deeply nested associations. .-< WorkPeriod Timecard -< Week -< Day -<--< Subtotal `-< Adjustment -< (has many) I'm using Active Model Serializer to build out the API. On the client side I want to load a timecard and all it's associations in one shot. Currently my serializers look like this, class TimecardSerializer < ActiveModel::Serializer embed :ids, include: true has_many :weeks end class WeekSerializer < ActiveModel::Serializer embed :ids,

has_many :through with a foreign key?

元气小坏坏 提交于 2019-12-18 10:35:28
问题 I've read multiple questions about this, but have yet to find an answer that works for my situation. I have 3 models: Apps , AppsGenres and Genres Here are the pertinent fields from each of those: Apps application_id AppsGenres genre_id application_id Genres genre_id The key here is that I'm not using the id field from those models. I need to associate the tables based on those application_id and genre_id fields. Here's what I've currently got, but it's not getting me the query I need: class

belongs_to with :class_name option fails

谁都会走 提交于 2019-12-18 10:33:31
问题 I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot! Here is a snip from my code. class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.text :name end end def self.down drop_table :users end end ##################################################### class CreateBooks < ActiveRecord::Migration def self.up create_table :books do |t| t.text :title t.integer :author_id, :null => false end

Rails - how to show attribute of an associated model

淺唱寂寞╮ 提交于 2019-12-18 09:37:49
问题 I am trying to make an app in Rails 4. I just asked this related question and got a clear answer. It seems I can't understand how to take that logic and apply it elsewhere. Rails How to show attributes from a parent object I have a user model, profile model a projects model and a universities model. Associations are: Profile belongs to university Profile belongs to user University has many profiles University has many projects Projects HABTM user Projects belong to universities In my projects

Associations and (multiple) foreign keys in rails (3.2) : how to describe them in the model, and write up migrations

ⅰ亾dé卋堺 提交于 2019-12-18 09:28:53
问题 I have 3 models: Question, Option, Rule Question has_many options; Option needs a foreign key for question_id Rule table consists of 3 foreign_keys: 2 columns/references to question_ids -> foreign keys named as 'assumption_question_id' and 'consequent_question_id' 1 column/reference to option_id -> foreign key named as option_id or condition_id Associations for Rule: Question has_many rules; and Option has_one rule I want to understand how to write up migrations for this, and how that

CakePHP 3 - Users belongsToMany Users

流过昼夜 提交于 2019-12-18 09:28:16
问题 I have a specific request, to build an association between users. This causes me confusion, how to reduce duplicate associations, query and results? The starting point would look like this? // UsersTable $this->belongsToMany('Users', [ 'through' => 'Connections', ]); How to fetch all associations in one query, regardless of whether users key in "user_from" or "user_to" field? 回答1: How about using aliases? Your users table: class UsersTable extends Table { public function initialize(array

Can't reload ActiveRecord association from controller

余生长醉 提交于 2019-12-18 07:19:29
问题 Spent a working day on this. I have class Box has_many :users, :through => :subscriptions end I also have custom insert_new_users and associate_with(new_users) methods which use multiple INSERT to do their job quickly. Anyway, they work fine. I also have this line at the end of "associate_with" method: def associate_with # mysql INSERT here self.users(true) # Should force reload end It works as expected when running in test environment (both controller and model tests) and it fails as

belongs_to and has_many to the same model

走远了吗. 提交于 2019-12-18 03:58:34
问题 I am wondering whether there is a way to do this with rails or not. Basically I have a user model and an event model. Event is created by a user and I want to have a foreign key (user_id) in the event model that indicates who created the event. Additionally, event can have many users who attend it so the event model becomes something like belongs_to :user has_many :users, :through => :guests #suppose i have the guest model and the user model looks something like has_many :events, :through =>

NHibernate - access the ID of an associated object without lazy loading the whole object

风流意气都作罢 提交于 2019-12-18 03:39:10
问题 I have two associated business objects - A and B. the association is (A->B)many-to-one, with B.Id a foreign key in A (so A has A.B_id in the DB). I'm using lazy=true and solved most of my problems, however in A's ToString I want to print also A.B.Id, which I should have without further trips to the DB. but accessing A.B activates the proxy, and since this isn't in the context of an open session, throws an exception. one easy but ugly solution would be to have A.B_id property. but that's part

Conditions in associated models using Model->find() (CakePHP)

北城以北 提交于 2019-12-18 02:50:08
问题 I am having some issues with CakePHP's find() method and conditions in 'deeper' model associations. There are some of these around but I could not find an answer to this so far. My model associations are User hasMany Post hasMany Comment hasMany Vote and Vote belongsTo Comment belongsTo Post belongsTo User respectively. The belongsTo associations use inner joins ('type' => 'INNER'). How do I find all comment votes for posts of a specific user with CakePHP's model->find() method? I used a