associations

Rails form with three models and namespace

社会主义新天地 提交于 2019-12-11 00:05:00
问题 Banging my head against this one for a long time. On Rails 2.3.2, Ruby 1.9.1. Trying to use one form to create three objects that have these relations: class Person has_one :goat end class Goat belongs_to :person has_many :kids end class Goat::Kid belongs_to :goat end Here's a summary of the schema: Person first_name last_name Goat name color Goat::Kid nickname age I'd like my #create action to instantiate new instances of all three models with the specified associations. However, while it

Rails 3. HABTM form select drop down menu

空扰寡人 提交于 2019-12-10 21:16:46
问题 I have an invoice form. This is a simplified version: so it has line items where you select a drop down menu of product names. This is working well: So the invoice-line_item relations is this: invoice has_many line_items and line_item belongs to invoice. line_item belongs to item and item has_many line_items. I have the items, line_items and invoice setup correctly. But now I want to add taxes to the line items. So I created a line_items_taxes table to create a HABTM relationship between line

I can't create model objects using accepts_nested_attributes_for. It won't create the nested object

故事扮演 提交于 2019-12-10 20:37:25
问题 My model structure looks like this: Board has_many Topics. Topic has_many Posts. app/models/board.rb class Board < ActiveRecord::Base has_many :topics end app/models/topic.rb class Topic < ActiveRecord::Base belongs_to :user belongs_to :board has_many :posts accepts_nested_attributes_for :posts validates :title, presence: true, length: { maximum: 255 } validates :user_id, presence: true validates :board_id, presence: true ... end app/models/post.rb class Post < ActiveRecord::Base belongs_to

rails has_many setter should set conditions if specified

不打扰是莪最后的温柔 提交于 2019-12-10 19:24:38
问题 This seems like a bug in Rails to me, but there's probably not much I can do about that. So how can I accomplish my expected behavior? Suppose we have: class User < ActiveRecord::Base has_many :awesome_friends, :class_name => "Friend", :conditions => {:awesome => true} end And execute the code: >> my_user.awesome_friends << Friend.new(:name=>'jim') Afterwards, when I inspect this friend object, I see that the user_id field is populated. But I would also expect to see the "awesome" column set

How to do HABTM management with auto completion in Rails?

℡╲_俬逩灬. 提交于 2019-12-10 19:21:33
问题 I am looking for a good solution for a probably typical problem of managing models with HABTM association in Rails. Let's assume that we have two models -- products and categories: Products has_many :categorizations has_many :categories, :through => :categorizations Categories has_many :categorizations has_many :products, :through => :categorizations Categorization belongs_to :product belongs_to :category Pat Shaughnessy is developing modified auto_complete plugin which can allow to manage

bulding association with has_many :through in multiple models

我的梦境 提交于 2019-12-10 18:47:07
问题 Please help to understand how to do @project.payments having this tree: Project |__Stages |__Costs |__Payments project.rb has_many :stages has_many :costs, :through => stages stage.rb belongs_to :project has_many :costs has_many :payments :through => costs cost.rb belongs_to :stage has_many :payments payment.rb belongs_to :cost 回答1: Note: As this is a nested has_many :through relationship, it'll only work in Rails 3.1+ (RC4 of 3.1 is out) project.rb has_many :payments, :through => costs 来源:

has_one :through => multiple

走远了吗. 提交于 2019-12-10 18:17:26
问题 Both Attendment & Vouching: belongs_to :event belongs_to :account Therefore: 1 to 1 relationship between attendments and vouchings. Is there a way to do this without my thinking too much? # attendment has_one :vouching :through => [:event, :account] Note: I don't mind thinking too much, actually. 回答1: Yeah i don't think you can use a has_one for this. Assuming I'm reading this correctly, you have two models: Attendment Vouching They both store an event_id and account_id. You want to know from

has_many through build

自古美人都是妖i 提交于 2019-12-10 17:53:36
问题 I have two models. User and Account as follows class Account < ActiveRecord::Base has_many :manages has_many :users, :through => :manages end class User < ActiveRecord::Base has_many :manages has_many :accounts, :through => :manages end If I were to use the rails console and create an instance of account by acc = usr.accounts.build acc.save The following command would return the account instance created usr.accounts But the following command would not return the user instance acc.users Also

Is it really needed to validate foreign keys?

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:22:30
问题 I am using Ruby on Rails v3.2.2 and, after post my previous question, I would like to know and understand if (or not) to explicitly validate foreign keys related to ActiveRecord::Associations is needed. For example: class CategoryAssociation < ActiveRecord::Base belongs_to :article, :foreign_key => 'article_id' belongs_to :category, :foreign_key => 'category_id' validates :article_id, :presence => true, :numericality => { :only_integer => true } validates :category_id, :presence => true,

Is there any way to use AREL for custom associations?

送分小仙女□ 提交于 2019-12-10 17:08:22
问题 model Post # ActiveRecord associations have tons of options that let # you do just about anything like: has_many :comments has_many :spam_comments, :conditions => ['spammy = ?', true] # In Rails 3, named scopes are ultra-elegant, and let you do things like: scope :with_comments, joins(:comments) end Is there any way to use AREL, or an otherwise leaner syntax, to define custom associations as elegantly as named scopes? update I've decided it's not a good idea to put that sort of detail into an