associations

Why am I getting this “undefined method `#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey” error?

落爺英雄遲暮 提交于 2019-12-09 17:38:51
问题 I have the following associations: class Question < ActiveRecord::Base has_and_belongs_to_many :footnotes has_and_belongs_to_many :pictures has_many :fields, :dependent => :destroy has_many :surveys, :dependent => :delete_all belongs_to :input belongs_to :element has_many :screenshots belongs_to :standard, :touch => true belongs_to :product, :touch => true belongs_to :condition, :class_name => "Field", :touch => true end class Product < ActiveRecord::Base has_many :questions, :dependent =>

Retrieve all posts where the given user has commented, Ruby on Rails

我与影子孤独终老i 提交于 2019-12-09 07:12:53
问题 I have users, posts and comments. User can post only one comment to each post. class User < ActiveRecord::Base has_many :posts has_many :comments end class Post < ActiveRecord::Base has_many :comments belongs_to :user end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end On userpage ( http://host/users/1 for example) I want to show all posts where the given user has commented. Each post then will have all other comments. I can do something like this in my User

Is it possible to ask for only certain columns from an ActiveRecord association?

落花浮王杯 提交于 2019-12-08 22:07:16
问题 consider def Foo has_one :user end let's say i only want a Foo 's User 's name, and not any of the other columns. so i want SELECT name FROM "users" WHERE "prices"."id" = 123 but doing foo.user.name will give me SELECT * FROM "users" WHERE "prices"."id" = 123 is there any slick way to use the association to get only one column? if not, then i have to do: User.where(id: foo.user_id).pluck(:name).first 回答1: In general you can specify what columns you want to select using the .select method,

How can I do complex entity associations queries in doctrine 2? (virtual entity)

放肆的年华 提交于 2019-12-08 18:05:02
问题 Let's say I have a blog application. The author can add multiple images to a post either by giving a link to a url of an existing image on the web, or upload a new image. I want to be able to back track from an image (either uploaded or url) to all the posts that uses that image, and from a specific post to all the images in that post (for example - so I could delete images that are illegal and/or suspend the post that uses them until the author fixes the post). However, I use different

Rails: :inverse_of and Association extensions

浪尽此生 提交于 2019-12-08 17:15:48
问题 I have the following set-up class Player < ActiveRecord::Base has_many :cards, :inverse_of => :player do def in_hand find_all_by_location('hand') end end end class Card < ActiveRecord::Base belongs_to :player, :inverse_of => :cards end This means the following works: p = Player.find(:first) c = p.cards[0] p.score # => 2 c.player.score # => 2 p.score += 1 c.player.score # => 3 c.player.score += 2 p.score # => 5 But the following doesn't behave the same way: p = Player.find(:first) c = p.cards

Overriding default identifier generation strategy has no effect on associations

夙愿已清 提交于 2019-12-08 17:00:28
问题 Symfony 2.7.2. Doctrine ORM 2.4.7. MySQL 5.6.12. PHP 5.5.0. I have an entity with custom ID generator strategy. It works flawlessly. In some circumstances I have to override this strategy with a "handmade" Id. It works when the main entity is being flushed without associations. But it doesn't work with associations. This example error is thrown: An exception occurred while executing 'INSERT INTO articles_tags (article_id, tag_id) VALUES (?, ?)' with params ["a004r0", 4]: SQLSTATE[23000]:

Rails 4.1.0 No Method error when trying to update a parent table from a child table

旧时模样 提交于 2019-12-08 13:57:50
问题 I have 2 rails models, a security and stock_quote model, which are as follows class StockQuote < ActiveRecord::Base belongs_to :security, class_name: "Security", foreign_key: 'security_id' def self.price_on(date) where("date(created_at) = ?", date).sum(:high) end feed_url = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values" def self.update_from_feed(feed_url) feed = Feedjira::Feed.fetch_and_parse(feed_url) unless feed.is_a?(Fixnum) add

Why doesn't accepts_nested_attributes_for allow_destroy not work?

最后都变了- 提交于 2019-12-08 13:48:43
问题 I have a nested attribute/resource, that I'm trying to destroy. What I want to do is to destroy a review object, when an associated life_hack article object is destroyed. Now I'm not too sure how the accepted_as_nested_attributes work but I'm pretty sure this is the way that I need to do it. Below i have some routes defined as so: Hacklife::Application.routes.draw do devise_for :users root 'life_hacks#index' resources :life_hacks, only: [:new, :index, :create, :show, :destroy] do resources

Question about Association and Models on Rails

被刻印的时光 ゝ 提交于 2019-12-08 13:40:53
问题 I have been working on a project recently where a Player can create a Team and be the Team Owner, but a player as well can be part of the Team by a separate table, named Squad. class Player has_many :teams has_many :squads end class Squad belongs_to :player belongs_to :team end class Team belongs_to :owner, :class_name => "Player" has_many :squads has_many :players, :through => "squads" end I don't know if this is all I need to make what I want, but I just can't figure out. How can I make the

Rails model associations, has_many :through and has_one with the same model

痞子三分冷 提交于 2019-12-08 12:08:55
问题 I have two models: User and State. The state model has records for each of the 50 states in the United States. I would like each User to have two attributes: one "home" state, and many states to "visit". I know I have to set up some sort of model associations to achieve this, but not sure when the best approach is. Here's what I have so far, but I know there must be something wrong with have has_many and has_one association to the same model. #user.rb class User < ActiveRecord::Base has_many