associations

has_one/has_many with dependent destroy but using a different name for the key

岁酱吖の 提交于 2020-01-05 11:59:13
问题 So I'm looking at someone's code which has the following (paraphrased): class user has_one :connection, :dependent => :destroy has_one :second_user, :through => :connection, :class_name => 'User' end class connection belongs_to :user belongs_to :second_user, :class => 'User' end If I have a connection object and delete the associated 'user' it can be destroyed fine. But I also want to make it so that if the User occupying the 'second_user' field is destroyed the connection is destroyed. How

Rails Paperclip with model association

烂漫一生 提交于 2020-01-05 08:00:04
问题 I have a page where I can view a product, and directly from this page the user can add and remove multiple images associated with the product. I'm using paperclip in a form to upload new file. Because multiple images can be saved for a product I created an image model belonging to the product model. It's not possible to use the default paperclip file input because of the association. My solution below is working, but I'm wondering if there's a better way in rails to accomplish this without

How to find all records with zero number of has_many assocations?

旧巷老猫 提交于 2020-01-05 07:51:27
问题 Let's say you have Question model with following has_many association:(example taken from this plugin) has_many :comment_threads, :class_name => "Comment", :as => :commentable, :dependent => :destroy How would I define a scope or a class method that returns questions that has no associated comments? Basically I want Question.unanswered to return all questions with zero comments. 回答1: I think approach with counter_cache is nicer and faster, but you can create the scope you want like that (you

update fail - undefined method `update_attributes' for #<ActiveRecord::Relation:

扶醉桌前 提交于 2020-01-05 07:13:25
问题 This follows on from my previous request which I answered. But my next issue which I cannot understand is why when my parameters report one record do I get a message that googling/SO searching suggest I need to use an update_all. has_many/belongs_to build association - why is insert statement for parameter blank? My update controller method is as follows: def update @role = Role.find(params[:id]) logger.debug "@role: id[#{params[:id]}], #{@role}, #{@role.inspect}, #{@role.to_s}" @permission =

Rails - Testing fixtures error NoMethodError: undefined method `type' for nil:NilClass

谁说我不能喝 提交于 2020-01-03 07:27:28
问题 I have a problem running tests that use fixtures with associations between models. Here's the error I get, as soon as I run rake test : ERROR["test_truth", SevenPortfolioTest, 0.005154775] test_truth#SevenPortfolioTest (0.01s) NoMethodError: NoMethodError: undefined method `type' for nil:NilClass ERROR["test_should_destroy_item_video", SevenPortfolio::ItemVideosControllerTest, 0.008887804] test_should_destroy_item_video#SevenPortfolio::ItemVideosControllerTest (0.01s) NoMethodError:

Rails 3 ActiveRecord::Relation random associations behavior

独自空忆成欢 提交于 2020-01-03 05:05:54
问题 I am currently having a strange issue with an application migrated from rails 2.3.8 to rails 3 (3.0.1, 3.0.2, 3.0.3). At random moments associations behave strangely. In some cases, an associated object will return the Relation object, instead of the corresponding model. This seems to happen mostly on polymorphic associations. For example: class A belongs_to :b, :polymorphic => true end class B has_many :a, :as => :source end When invoking "a.b" this will "sometimes" return the Relation

How to have a collasped drop down list in rails simple_form

假装没事ソ 提交于 2020-01-03 03:54:10
问题 In my app, there are two models: rfq and standard. Their relationship is many-to-many. In rfq creating screen, the code below displays a list of available for selection in drop down list: <%= simple_form_for @rfq do |f| %> <%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %> <% end %> The problem is that the list is not collapsed, which means there are multiple standards displayed in a multi-line boxes. How can

Has_Many Through Associations and Nested Attributes

二次信任 提交于 2020-01-03 01:53:07
问题 I'm trying to create a view allowing me to create and edit values of my joining table directly. This model is called 'hires'. I need to be able to create multiple rows in my joining table for when a child hires up to 2 books. I'm having some trouble and I suspect it's down to my associations... I have 3 models. Each Child can have 2 books: class Child < ActiveRecord::Base has_many :hires has_many :books, through: :hires end class Hire < ActiveRecord::Base belongs_to :book belongs_to :child

How to prune duplicate associations to yield a unique most-complete set

。_饼干妹妹 提交于 2020-01-03 01:51:26
问题 I hardly know how to state this question, let alone search for answers. But here's my best shot. Assume I have a table Col1 Col2 -----+----- A | 1 A | 2 A | 3 A | 4 B | 1 B | 2 B | 3 C | 1 C | 2 C | 3 D | 1 I want to find the subset of associations (rows) where: There are no duplicates in Col1 There are no duplicates in Col2 Every value in Col1 is associated with a value in Col2 So the above example could yield this result Col1 Col2 -----+----- A | 4 B | 2 C | 3 D | 1 Notice that A-4 must be

Rails: Finding all associated objects to a parent object

坚强是说给别人听的谎言 提交于 2020-01-02 10:28:29
问题 I have created a complex object in rails with a principle parent object "Resume" it has a number of child objects for each section("objective_section", "contact_section", etc), is there a way I can fetch all associated objects to the parent object Resume? 回答1: If by fetch you mean load from the database all in one query, then sure: Resume.first(:include => [:objective_sections, :contact_sections]) # etc... If this is a common pattern and you want to DRY things up without much effort, you can