Association through another table "Unknown key: through"(Rails) - ruby-on-rails

I'm creating trips in my rails app and i want to add categories to those trips. The categories are stored in the Categories table in my DB and the user may select which categories are suitable for the trip. So multiple categories a trip can be used.
Although i'm a noob i figured some things out with some help of the RoR guides about this subject. Now, i've got a 3rd table tripcategories which will have to hold the trip_id and the category_id. Right? With that i've got the following models:
trip.rb:
class Trip < ActiveRecord::Base
attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo
has_many :triplocations, :dependent => :destroy
has_many :tripcategories
has_many :categories, :through => :tripcategories
accepts_nested_attributes_for :triplocations, allow_destroy: true
end
category.rb:
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
belongs_to :trip, :through => :tripcategories
end
tripcategory.rb:
class Tripcategory < ActiveRecord::Base
attr_accessible :category_id, :trip_id
belongs_to :trip
belongs_to :category
end
When i'm trying it this way and trying to call trip.categories in my trip index it says "Unknown key: through". Am i doing something horribly wrong or am i missing the bigger picture?
Thanks in advance!

class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
has_many :trips, :through => :tripcategories
end

Related

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation How can i get this relationshop objects?

As i mentioned on the question..
I know this error and i know how associations work
In my case i have ProductRepository model which belongs_to :category
So of course when i'm trying to delete this category which this ProductRepository object belongs to i should get this error and it's totally fine and i know how to handle this exception
Now let's say that i have a many models which referring to categories like
Product, ProductRepositoy, Post, etc...
My question here, how can i get a list of this objects which belongs to categories, or even get a class name array or whatever
So when i handle this exception i can send a message to ask the user to check and delete the following before deleting this category ?
Example:
Once user delete category... if it has any items which belongs to it
he should get a message saying Please check the following (Posts, Products, etc..)
For who is going to answer, all i need is how to get this objects or even class names only
And i can do the rest
Thanks in advance :)
Here is the file, after removing what isn't necessary for the question
category.rb
class Category < ActiveRecord::Base
has_many :brands, :dependent => :destroy
has_many :products, :dependent => :destroy
end
product_respository.rb
class ProductRepository < ActiveRecord::Base
belongs_to :category
has_many :unity_folders, as: :product, dependent: :destroy
validates :name, :price, :barcode, presence: true
validates :barcode, uniqueness: true
end
product.rb
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :brand
belongs_to :organization
has_many :unity_folders, as: :product, dependent: :destroy
has_one :product3d
belongs_to :shop
end

Rails relationship between three models confusing

I know there is a plenty of tutorials explaining how to create a 'has_many through' relationship between models, but I think my question is both technical and conceptual.
The objective is to create an online food ordering website
I created the Order, Item and OrderItem models.
Relationships:
class OrderItem < ActiveRecord::Base
belongs_to :item, conditions: "active = true"
belongs_to :order
end
class Order < ActiveRecord::Base
belongs_to :user
has_many :order_items
has_many :items, through: :order_items
validates :status, inclusion: { in: %w(ordered completed cancelled) }
end
class Item < ActiveRecord::Base
has_and_belongs_to_many :categories, join_table: :items_categories
has_many :order_items
has_many :orders, through: :order_items
validates_presence_of :title, :description
validates :price, numericality: { :greater_than=>0 }
end
Am I doing something wrong? Each order should be able to contain many items and the quantity of them.
I'm not very positive I'm doing the correct architecture for these models, as I can't assign the quantity via << operator, only assign the item.
Thanks for your time.
like this
order = Order.new(user: #user)
order.order_items << OrderItem.new(quantity: 100, item: Item.first)

FactoryGirl with has_many :through associations with capybara

I have the following models:
class Productmainclass < ActiveRecord::Base
attr_accessible :name, :id, :maintext
has_many :producttaggings, :dependent => :destroy
has_many :products, :through => :producttaggings
has_many :productsubclasses
end
class Productsubclass < ActiveRecord::Base
attr_accessible :name, :id, :maintext
has_many :producttaggings, :dependent => :destroy
has_many :products, :through => :producttaggings
belongs_to :productmainclass
end
class Product < ActiveRecord::Base
attr_accessible :name, :productimage, :size, :description, :price
has_many :producttaggings, :dependent => :destroy
has_many :productsubclasses, :through => :meteoritetaggings
has_many :productmainclasses, :through => :meteoritetaggings
mount_uploader :productimage, ProductimageUploader
end
class Producttagging < ActiveRecord::Base
belongs_to :product
belongs_to :productsubclass
belongs_to :productmainclass
attr_accessible :product_id, :productsubclass_id, :productmainclass_id
end
I now want to create a Product with FactoryGirl and Capybara. In the spec I simply have:
product = FactoryGirl.create(:product)
In my factories.rb I have:
factory :product do
name "Blue glass"
description "Description text of product"
productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb')
productsubclass
productmainclass
end
factory :productsubclass do
name "Colored glasses"
productmainclass
end
factory :productmainclass do
name "Glasses"
end
Running the test I get:
Failure/Error: product = FactoryGirl.create(:product)
NoMethodError:
undefined method `productsubclass=' for #<Product:0xcd42090>
I think the way you have it setup would work if you were dealing with a situation where :product belonged to productsubclass, then the product and the productsubclass would be created with the product.productsubclass_id nicely inserted and all would be fine, but that's clearly not your structure, so we'd have to use another way. I think the link that #depa noted is the right way to go, specifically the 'Basic has many associations' section in this document: http://robots.thoughtbot.com/aint-no-calla-back-girl although you have the added complexity of a has_many through. But essentially your looking at a situation where you create an object and then after that you trigger another create to make the many's. Hope this makes sense :)
** Update **
Here's another approach which might be a little limited but you could just create the records from the other direction. So, if you just want one record in each object/table how about this:
FactoryGirl.define do
factory :producttagging do
product
productsubclass
productmainclass
end
end

Accessing rails attributes through deep associations

Here are my relevant models:
class ListItem < ActiveRecord::Base
belongs_to :inventory_item
belongs_to :shopping_list
belongs_to :item
end
class ShoppingList < ActiveRecord::Base
has_many :list_items
belongs_to :user, :foreign_key => :user_id
end
class InventoryItem < ActiveRecord::Base
belongs_to :item, :foreign_key => :item_id
belongs_to :vendor
has_many :list_items
end
I'm trying to access attributes of InventoryItem in my view. Here's what I currently have in my ShoppingListController.
def show
#list_items = ShoppingList.find(params[:id]).list_items
end
Can I do something like #inventory_items = #list_items.inventory_items? That code and variants of it that I've tried haven't worked. What am i missing here? Any tips for accessing attributes through multiple models like this? Thanks in advance!
The most straight forward approach would be to use has_many through on the ShoppingList class:
has_many :inventory_items, through: :list_items

Rails 'DB - no such column' error for has_many through relationship

I created a has_many :through relationship for a rails app I am creating. However, I am unable have nested attributes for the many to many relationship.
My models are as follows:
article.rb
class Article < ActiveRecord::Base
attr_accessible :body, :title
has_many :article_tags
has_many :tags, :through => :article_tags, :foreign_key => :tag_id
accepts_nested_attributes_for :tags, reject_if: :all_blank
attr_accessible :tags_attributes
validates_presence_of [:body, :title]
end
tag.rb:
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, :through => :article_tags, :foreign_key => :article_id
attr_accessible :name
validates_presence_of :name
accepts_nested_attributes_for :articles, reject_if: :all_blank
attr_accessible :articles_attributes
end
article_tag.rb:
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
Now in my view for the article (show.html.slim), I want to show all tags that belong to the article using the line:
- #article.tags.each do |t|
= t.name
However, I get the error:
SQLite3::SQLException: no such column: article_tags.article_id: SELECT "tags".* FROM "tags" INNER JOIN "article_tags" ON "tags"."id" = "article_tags"."tag_id" WHERE "article_tags"."article_id" = 1
This problem is solved.
For anybody who needs to know:
You must manually add the foreign keys in the migration and the schema. I added them and voila! it works!
Thanks to Dan Reedy for his help!
Your ArticleTag class is incorrect. The belongs_to associations should be singular.
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
You must use has_and_belongs_to_many in this situation.

Resources