model-associations

Can't access field through 3 models in Rails 5

廉价感情. 提交于 2020-02-05 03:49:08
问题 I'm trying to do something very similar to this question I have 4 models, one of which (CoffeeBlend) is a join table: class CoffeeRoast < ApplicationRecord has_many :coffee_blends has_many :coffee_beans, through: :coffee_blends has_one :country, through: :coffee_beans end class CoffeeBean < ApplicationRecord has_many :coffee_blends has_many :coffee_roasts, through: :coffee_blends belongs_to :country end class Country < ApplicationRecord has_many :coffee_beans end class CoffeeBlend <

Association between Category, Subcategory, and Lawyer

末鹿安然 提交于 2020-01-13 04:55:47
问题 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 do not want to see a column on Subcategories Table referencing Categories. Not all Categories has Subcategories. i.e. some don't have subcategories as seen in the picture. I have 2 separate forms creating category and subcategory. I added category_id

Association between Category, Subcategory, and Lawyer

泄露秘密 提交于 2020-01-13 04:55:06
问题 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 do not want to see a column on Subcategories Table referencing Categories. Not all Categories has Subcategories. i.e. some don't have subcategories as seen in the picture. I have 2 separate forms creating category and subcategory. I added category_id

get record with at least one associated object

混江龙づ霸主 提交于 2020-01-11 11:37:33
问题 I have the following schema in mongoid : User has many tasks - has_many :tasks Task belongs to user - belongs_to :user How can I get only 10 first users with at least one task? Something like this: User.where(:tasks.ne => [] ).limit(10) 回答1: Your problem is that Mongoid's has_many doesn't leave anything in the parent document so there are no queries on the parent document that will do anything useful for you. However, the belongs_to :user in your Task will add a :user_id field to the tasks

RnR: Database normalization, rails models and associations

末鹿安然 提交于 2020-01-06 12:23:41
问题 Ok, so I have three different objects: a person, place, and equipment. Each can have an address, in most cases multiple address and multiple phone numbers. So my thought is create the specific object tables, the have an address and a phone table. Here my question: So typically, in my SQL world, I would just have an object_id column in the address and phone table and put a id of the object in the object id column, and then select all the address or phone records that match. I could do this, do

Rails - How to manage nested attributes without using accepts_nested_attributes_for?

放肆的年华 提交于 2020-01-01 03:15:15
问题 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 should my form, controller and models look like if I want to mimmic and augment accepts_nested_attributes_for? The real trick is I need to be able to update both existing AND new models with existing associations/attributes. I'm building an app

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

纵然是瞬间 提交于 2019-12-30 06:53:51
问题 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 (Rails 3.0), partly surely due to lack of knowledge (I hope, at least - that would be easy to solve). Example: An InternalMessage class. Messages can be sent between users (Recipients of an InternalMessage, and 'deletions' of messages, are stored in

n:m self-join with ruby on rails active record

被刻印的时光 ゝ 提交于 2019-12-25 12:55:23
问题 I'm trying to accomplish the following with ruby on rails, and I have problems to get the model-configuration right: I would like to model possible connections between airports. I created the following models: > rails generate model airport city name > rails generate model connection airport_id destination_id But most of the n:m association examples deal with associations between two different models on the one hand, or self-referencing models on the other hand, but I want to model a n:m

Association Class vs. Attribute

谁都会走 提交于 2019-12-24 12:04:30
问题 Let's assume I have 2 classes with association relation. What is the difference between adding attributes to one of the classes and adding an Association Class with attributes? I understand that association class describes the association relation, but cannot I use simple class attributes instead? What are the added values of Association Class? 回答1: Association and attributes are actually different renderings of the same thing. The difference is more a bit of esoteric nature. An association

get parent values in child model

社会主义新天地 提交于 2019-12-24 10:01:47
问题 I have a model called RsvpRegistrations with belongs_to :rsvp I need to use values from the parent 'rsvp' object in my validations such as validates_presence_of :phone if self.rsvp.phone (Rsvp.phone is boolean) But this doesn't work. The error I get is undefined method `rsvp'. How can I access the parent object and its values? Once I get it working, I have other similar validations to run, so I'm thinking I need to grab the parent 'rsvp' one time and then reference it in my other validations.