has-many

Backbone-relational hasmany best practices

断了今生、忘了曾经 提交于 2019-12-05 01:08:02
I am new to Backbone-relational, I am not sure what is the right way to use HasMany. I have a Parent model which have many children (by "many" I mean thousands of children). In order to avoid performance issue, I query children by their foreign key: /child/?parent=1 , instead of create a huge list of child_ids in Parent . But seems this is not the way Backbone-relational work. So I am wondering what is the right way to handle this. 1, Change my json api to include list of child id in parent, then send thousands of ids as Backbone-relational recommend: url = function(models) { return '/child/'

Rails Question: belongs_to with STI — how do i do this correctly?

自作多情 提交于 2019-12-05 00:07:41
I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many :babies end class Father < Parental has_many :babies end class Baby < ActiveRecord::Base belongs_to :?????? end What should Baby belong_to? In terms of a migration, what should i name/add for foreign key on the babies table? I've had a hard time researching this, is there a definitive source that explains this? The API docs did not seem to hit it on the head OR

How to use constant in the ON condition in Yii2 hasMany relation

大憨熊 提交于 2019-12-04 22:18:55
I try to create a polymorphic association, what is common in Rails but unfortunately not in Yii2. As part of the implementation I need to define the relation: public function getImages() { return $this->hasMany(RecipeImage::className(), ['imageable_id' => 'id', 'imageable_type' => 'Person']); } But this doesn't work, because 'Person' is treated as an attribute of the current model, but it is a constant (class name for the polymorphic association). If I try to use 'andWhere' it adds the condition of course in a WHERE clause instead of the ON clause, causing that only records with existing image

Rails: has_many with extra details?

喜欢而已 提交于 2019-12-04 19:41:12
While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base # recipe name, etc. end If we just wanted to associate Recipes with Ingredients, that's as simpling as

Rails Filter records of child model based upon the parent model attribute

强颜欢笑 提交于 2019-12-04 19:13:34
Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads with published foto_gossip like this: Upload.with_published_foto_gossip 来源: https://stackoverflow.com

Access extra attributes in join table when using though in has_many relationship

六月ゝ 毕业季﹏ 提交于 2019-12-04 18:31:15
I was considering adding some extra properties between a has_many relationship. For example, I have a Users table, and a Group table. Users can join groups via a :through has_many relationship. I want to add the property of the 'role' of the user in that group. create_table "groupization", :force => true do |t| t.integer "user_id" t.integer "group_id" t.string "role" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end I was wondering, how can I access the role attribute. I was thinking something like: user.groups[0].role Is that a correct approach? I know the

Can has_one association be used when the model has one or zero instances of another model?

耗尽温柔 提交于 2019-12-04 18:17:29
问题 RailsGuides says: http://guides.rubyonrails.org/association_basics.html A has_many "association indicates that each instance of the model has zero or more instances of another model." "A has_one association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model." Does that mean if I want to set up an association that each

When are Active Record objects in has_many relationships saved?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 14:51:22
I'm using Rails 1.2.3 (yeah, I know) and am confused about how has_many works with respect to object persistence. For the sake of example, I'll use this as my declaration: class User < ActiveRecord::Base has_many :assignments end class Assignment < ActiveRecord::Base belongs_to :user end As I understand it, this generates, among others, a method User#assignments.build , which creates an Assignment object whose user_id is the receiving instance's id (and whose other fields are as specified in the argument), but does not save this object in the database. The object can be saved later by calling

How to persist hasMany association in a single Ember.js form using Ember Data & Rails?

♀尐吖头ヾ 提交于 2019-12-04 13:21:35
问题 I'm having trouble determining the correct way to persist a hasMany association with a single form using Ember.js, Ember Data and Rails. A Client hasMany Projects. I have a new project form that has two fields: project name and client name. http://cl.ly/image/3z0P0R3M1t2u I've tried to keep my create logic within the Ember.js ClientsController & ProjectsController , but will I need to move some of that to the submit action on my ProjectsNewView ? Update: I've updated my code after finding

Elegantly selecting attributes from has_many :through join models in Rails

淺唱寂寞╮ 提交于 2019-12-04 09:57:36
问题 I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is. Lets say we have Items, Catalogs, and CatalogItems with the following Item class: class Item < ActiveRecord::Base has_many :catalog_items has_many :catalogs, :through => :catalog_items end Additionally, lets say that CatalogueItems has a position attribute and that there is only one CatalogueItem between any catalog and any item. The most obvious but slightly