has-many

When will ActiveRecord save associations?

独自空忆成欢 提交于 2019-12-04 08:48:27
问题 1) I know that it will save associations when autosave: true as per http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/AutosaveAssociation.html 2) I know that it will save associations that are constructed like book = Book.new(name: 'foo') book.authors.build(name: 'bar') #has_many book.save or like book = Book.new(name: 'foo') book.build_author(name: 'bar') #has_one book.save 3) I think associations are also saved when they are assigned or added book = Book.new(name: 'foo') book.author

Error while using `find_or_create_by` on a `has_many` `through` association

夙愿已清 提交于 2019-12-04 06:04:18
I am running in to a problem while using find_or_create_by on a has_many through association. class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role < ActiveRecord::Base # DB columns: user_id, role_id has_many :permissions has_many :users, :through => :permissions end class User has_many :permissions has_many :roles, :through => :permissions end Rails throws an error when I invoke find_or_create_by on roles association of a User object. u = User.first u.roles.find_or_create_by_rolename("admin") # Rails throws the following error # NoMethodError: undefined method

EmberJS: Unable to get the length of an hasMany array two levels down

醉酒当歌 提交于 2019-12-04 04:59:58
Im trying to create a computed property to get me the sum of the length of all pages. But i cannot figure out how to access a child so i can get the childs of that child. App.Document = DS.Model.extend({ name: DS.attr('string'), spreads: DS.hasMany('App.Spread'), pagesCount: function() { // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length. var spreadsLength = this.get('spreads.length'); var firstSpread = this.get('spreads')[0]; return firstSpread.get('pages.length'); }.property('spreads') }); App.Spread = DS.Model.extend({ document: DS

Two has_many links between the same models

白昼怎懂夜的黑 提交于 2019-12-04 02:15:49
问题 I have users which have products through a habtm link, which is working. I want to add a link between the user model and the product model, to keep track of the creator of that product (who doesn't always own the product, of course) But when I write in my user and product models a new link, the application screws up because I can't distinguish the creator of a product from the owner of (a lot of) products . Can you help me ? Here is my models : class Product < ActiveRecord::Base belongs_to

Rails has_many through for has_many with multiple models

三世轮回 提交于 2019-12-03 21:39:36
What would be the best way to model the following situation: Word belongs_to :wordable, :polymorphic => true Phrase has_many :words, :as => :workable belongs_to :story Line has_many :words, :as => :wordable belongs_to :story Story has_many :lines has_many :phrases has_many :words, :through => :phrases has_many :words, :through => :lines I want to be able to do @story.words to get list of all words that are linked to a story either via lines or via phrases... Is that possible? Harish Shetty Try this: class Story has_many :lines has_many :phrases def words(reload=false) @words = nil if reload

Rails - check if record exists in has_many association

笑着哭i 提交于 2019-12-03 19:53:15
问题 I'm not sure if my question is worded correctly. I have three models: User , Item , and UserItem . user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item belongs_to :user user_item belongs_to :user user_item belongs_to :item I need a way to see if a user has an item to make if statements in my item views But here's the catch, user_items have enum status: [ :pending, approved] . So I need to see if

Rails has_many through aliasing with source and source_type for multiple types

走远了吗. 提交于 2019-12-03 16:59:23
问题 So here is a sample class class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @company.angels and @company.vc_firms works as expected. But how would I have @company.investors that are comprised of both source types? That would work for all polymorphics on the investor column of the Investments table? or perhaps a

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

跟風遠走 提交于 2019-12-03 12:15:39
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 instance of the model has zero or one instance of another model, the best way is to use has_many and not

What is the “rails way” to enforce a has_many but has-only-one-current association?

本小妞迷上赌 提交于 2019-12-03 11:08:38
问题 I have a simple rails app with models project and phase. A project has many phases, but only on phase can be active (i.e. "current") at a time. I still want the other phases to be accessible, but the current phase should be the main anchor for the application. The decision on how to implement this requirement has major implications on how I handle model access, validations and views / forms for creation update. So the question is: How do I achieve this "has_many but has-only-one-current

Grails dynamic scaffold with hasMany: is it a bug or am I misconfiguring?

让人想犯罪 __ 提交于 2019-12-03 09:51:51
问题 I'm a Grails noob and running into something that seems to be a bug, but it is entirely possible I'm not configuring everything correctly. I've got two simple Domain Classes: class Player { String firstName String lastName static constraints = { firstName(blank:false) lastName(blank:false) } String toString() { lastName + ", " + firstName } } and class Team { String mascot; static hasMany = [players:Player] static constraints = { mascot(blank:false) } } I have controllers for both that do