activerecord

ActiveRecord has two association

心不动则不痛 提交于 2019-12-21 17:10:15
问题 What is the best way to achieve a has two association with activerecord? I have a Team and Game models. Each Team will have_many games @team.games . A Game will have two teams @game.hosting_team and @game.opposing_team . I started out with two belongs_to/has_one associations but then @team.games would only return their home games. The other option I can think of is using a HABTM and use a validator to ensure there are only records. The only thing missing is keeping track of the home team. It

ActiveRecord::UnknownAttributeError

二次信任 提交于 2019-12-21 16:52:15
问题 I'm trying to create hotel with some fields, one of the fields is photo, i want to use multiple files upload with carrierwave and nested_form. I found this article and have some result. When i'm on /hotels/new, filling fields, choosing photos and press submit, getting ActiveRecord::UnknownAttributeError in HotelsController#create unknown attribute: attachable_type. Console Started POST "/hotels" for 127.0.0.1 at 2013-09-27 17:35:18 +0300 Processing by HotelsController#create as HTML

Ruby On Rails 5, activerecord query where model association ids includes ALL ids in array

不问归期 提交于 2019-12-21 16:45:10
问题 I have a Has many belongs to association for Recipes and Ingredients. I am trying to return recipes which have all the ingredient ids in a given array of integers. Eg. I search using multiple ingredients, and I want to return any Recipe that has all the ingredients. If too many ingredients are given, but the Recipe includes them all, it should also return the recipe. I have tried a few things, Recipe.joins(:ingredients).where(ingredients: { id: ids }) which returns the Recipes that have ANY

Ruby On Rails 5, activerecord query where model association ids includes ALL ids in array

自闭症网瘾萝莉.ら 提交于 2019-12-21 16:45:01
问题 I have a Has many belongs to association for Recipes and Ingredients. I am trying to return recipes which have all the ingredient ids in a given array of integers. Eg. I search using multiple ingredients, and I want to return any Recipe that has all the ingredients. If too many ingredients are given, but the Recipe includes them all, it should also return the recipe. I have tried a few things, Recipe.joins(:ingredients).where(ingredients: { id: ids }) which returns the Recipes that have ANY

Difference between ActiveRecord and ActiveRecord::Relation objects

半腔热情 提交于 2019-12-21 14:51:32
问题 I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object. I have understand that ActiveRecord is the single object find by something like User.find(1) And ActiveRecord::Relation is the array like object Find by something like User.where(id: 1) I am looking for the difference between them in terms of query execution or deep explanation about them, so it will clear the whole concept behind it. Thanks in advance! 回答1: An

Why Active Record relation is not returned in console?

爱⌒轻易说出口 提交于 2019-12-21 12:58:47
问题 I have finally started upgrading my Rails apps from 2.3.8 to 3.1.0. I was watching RailsCasts (http://railscasts.com/episodes/202-active-record-queries-in-rails-3) about Active Record queries. When I open up the console (rails c) and do query similar to this: articles = Article.order("name") Instead of returning Active Record relations, I see the query executed. What am I doing wrong here? Rails version: 3.1.0 RVM on 1.9.2 Thank you for your help! EDIT: I have added a screenshot from the

New model object through an association

陌路散爱 提交于 2019-12-21 12:41:23
问题 I thought it was possible to create a new model object through an association. class Order < ActiveRecord::Base belongs_to :basket end class Basket < ActiveRecord::Base has_one :order end order = Order.new() basket = order.basket.new() # NoMethodError: undefined method `new' for nil:NilClass 回答1: It is, but your syntax is a little wrong: class Order < ActiveRecord::Base belongs_to :basket end class Basket < ActiveRecord::Base has_one :order end order = Order.new() basket = order.create_basket

ORM: OneToOne mapping on Non Primary-Key Join column - Book and Inventory mapped by ISBN

偶尔善良 提交于 2019-12-21 12:35:20
问题 I have a Book model and Inventory model mapped by ISBN number, but ISBN is not the primary key in either. Books belong to Bookstores and Inventory is for a group of Bookstores(BookstoreChain). Inventory is shared by all Bookstores belonging to a BookstoreChain. I'm using Hibernate @OneToOne mapping on the book side to fetch inventory info by joining the ISBN column. Somehow, Hibernate generates the left outer join query correctly, but inventory is null on the Book object. Its not lazy loaded

How can I access an ActiveRecord grandparent association through a parent association that has not yet been saved?

巧了我就是萌 提交于 2019-12-21 12:31:46
问题 I have a situation where I would like access to an associated grandparent before the parent object is saved. I can think of several hacks, but I'm searching for a clean way to accomplish this. Take the following code as an illustration of my problem: class Company < ActiveRecord::Base has_many :departments has_many :custom_fields has_many :employees, :through => :departments end class Department < ActiveRecord::Base belongs_to :company has_many :employees end class Employee < ActiveRecord:

When does ActiveRecord establish connections?

亡梦爱人 提交于 2019-12-21 12:14:19
问题 In some of my Rails applications, my ActiveRecord models seem to establish db connections on initialization (e.g., when I do rails console ), while in others connections seem to be established only when I reference the model class or instantiate a model object. For example, I just went to one application, opened Rails console and wrote: SomeModel.connected? and it returned false . I went to another application, entered the same command (for a different model), and it returned true . I went to