I am working on my very first rails app so please pardon me if the answer is obvious.
My userlocation model looks like this
class UserLocation < ActiveRecord::Base
belongs_to :uid, :class_name => User, :foreign_key => "uid"
has_one :current_location, :class_name => Location, :foreign_key => "city"
has_one :destination, :class_name => Location, :foreign_key => "city"
validates :user, presence: true
end
When I try to put something into this model I get the said error. What am I doing wrong?
I think the class_name option should be a string, not a class.
Try using :class_name => "User" and :class_name => "Location" in your associations.
See examples on http://guides.rubyonrails.org/association_basics.html
Related
I have two models which are related with has_many :through association.
I have user & software_license models associated through user_software_license model. I am trying to save data using nested form, but I am getting errors
my models :
user.rb
has_many :software_licenses, through: :user_software_licenses
has_many :user_software_licenses
accepts_nested_attributes_for :software_licenses, :allow_destroy => true
software_license.rb
has_many :users, through: :user_software_licenses
has_many :user_software_licenses
accepts_nested_attributes_for :user_software_licenses
user_software_license.rb
belongs_to :user
belongs_to :software_license
my nested form :
= f.fields_for :software_licenses do |software_license|
= render :partial => "users/software_licenses", :locals => {:f => software_license}
= link_to_add_fields "Add", f, :user_software_licenses, true, "users/software_licenses"
this is how I am initialising it in my controller
user.software_licenses.build
and when I click on 'add more' button in nested form after submitting form I am getting parameters like this :
"software_licenses_attributes"=>{"0"=>{"id"=>"2"}},
"user_software_licenses"=>{"id"=>"2"},
I have two types of users, one that can create movies and one that can create reviews:
class User < ActiveRecord::Base
has_many :created_movies, foreign_key: 'creator_id', class_name: 'Movie'
has_many :reviewed_movies, foreign_key: 'reviewer_id', :through => 'Review'
end
class Review < ActiveRecord::Base
belongs_to :movie
belongs_to :reviewer, class_name: 'User'
end
class Movie < ActiveRecord::Base
has_many :reviews
belongs_to :creator, class_name: 'User'
end
Whenever I try to run the following in my users/show:
<% if #user.reviewed_movies.any? %>
I get this problem:
ActiveRecord::HasManyThroughAssociationNotFoundError at /users/1
Could not find the association "Review" in model User
I can see that I can successfully populate the reviewer_id with the correct user when the review is created when I go into the command line:
m = Movie.last
m.reviews[1]
# => <Review:0x007fdc4036a938> {
# :id => 2,
# :rating => 2,
# :title => "bye",
# :content => "byeeeee",
# :created_at => Tue, 15 Sep 2015 19:20:25 UTC +00:00,
# :updated_at => Tue, 15 Sep 2015 19:20:25 UTC +00:00,
# :movie_id => 3,
# :reviewer_id => 1
# }
But I can't retrieve it from the other end. If anyone can provide some assistance it would be greatly appreciated, thanks!
ActiveRecord::HasManyThroughAssociationNotFoundError at /users/1 Could
not find the association "Review" in model User
The problem is here in this line
has_many :reviewed_movies, foreign_key: 'reviewer_id', :through => 'Review'
Which should be
has_many :reviewed_movies, foreign_key: 'reviewer_id', class_name: 'Review'
You need to tell your User model it has many reviews through which it has many movies called "reviewed_movies".
class User < ActiveRecord::Base
has_many :created_movies,
foreign_key: :movie_id,
class_name: 'Movie'
has_many :reviews
has_many :reviewed_movies,
foreign_key: :movie_id,
through: :reviews,
class_name: 'Movie'
end
EDIT: The foreign key is there to tell your model which foreign key to use, if your relation isn't called the same as the foreign-key, so I think it should be 'movie_id'... Ok, not sure about this any longer, I guess it depends on your setup ^^
I have a Store model, Product model, and a StoreProduct model to form a many-to-many relationship between stores and products.
The StoreProduct model, on top of store_id and product_id, has an attribute price:float.
I'm struggling to figure out how I can design a form to create a new product given the store. Is it better off making a form for a new StoreProduct object? What is the conventional way of doing this?
store.rb
class Store < ActiveRecord::Base
validates :name, presence: true
has_many :store_products
has_many :products, through: :store_products
end
product.rb
class Product < ActiveRecord::Base
validates :name, presence: true
validates_numericality_of :price, on: :create
has_many :reviews
has_many :users, through: :reviews
has_many :store_products
has_many :stores, through: :store_products
end
store_product.rb
class StoreProduct < ActiveRecord::Base
belongs_to :store
belongs_to :product
end
I'm working through creating a has_many: through relationship in active admin. Here are the models as they stand:
class Category < ActiveRecord::Base
has_many :subcategories
end
class Subcategory < ActiveRecord::Base
has_many :product_in_subcategories
has_many :products, through: :product_in_subcategories
accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true
belongs_to :category
end
class Product < ActiveRecord::Base
has_many :product_in_subcategories
has_many :subcategories, through: :product_in_subcategories
accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true
end
class ProductInSubcategory < ActiveRecord::Base
belongs_to :product
belongs_to :subcategory
end
In ActiveAdmin I have the permit_params and form like so:
ActiveAdmin.register Product do
# note some params that are product only have been removed for simplicity
permit_params :name, subcategory_id:[:id], product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update]
form do |f|
f.inputs
f.has_many :product_in_subcategories do |s|
s.input :subcategory_id, :as => :check_boxes, :collection => Subcategory.all
end
f.actions
end
end
The form populates as should, and will save everything except for the subcategory_id. If I enter into the DB a proper subcategory_id the box will show checked on edit.
The messages when saving give:
Unpermitted parameters: subcategory_id
However, it appears it is trying to submit this with the product, for which there isn't a subcategory_id. Any ideas on what I am doing incorrectly here? This is driving me nuts and I've read everything I can find. I'd really like to understand what I'm doing wrong. Thanks.
After much time spent on this one, I couldn't find a suitable solution except for this one, which is actually very nice. It in fact is not much different from my envisioned solution:
The only changes to the above code were made in ActiveAdmin:
ActiveAdmin.register Product do
# note some params that are product only have been removed for simplicity
permit_params :name, product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update]
form do |f|
f.inputs
f.has_many :product_in_subcategories do |s|
s.input :subcategory_id, :as => :select, :collection => Subcategory.all
end
f.actions
end
end
Very strange how this allows a select box with no issues, but it flips out over check boxes. Nonetheless, I'm happy with the solution.
I have the following two simple classes:
class Person < ActiveRecord::Base
has_many :parent_child_assignments
has_many :children, :through => :parent_child_assignments
has_many :parents, :through => :parent_child_assignments
end
class ParentChildAssignment < ActiveRecord::Base
belongs_to :parent, :class_name => "Person"
belongs_to :child, :class_name => "Person"
end
created from the following rails commands
rails generate model Person name:string
rails generate model ParentChildAssignment parent_id:integer child_id:integer
I can get one direction (joe.children or joe.parents) to work but not both
Someone must have asked this question before, but I can't find it.
Thanks in advance.
The classes should look like:
class Person < ActiveRecord::Base
has_many :parent_child_assignments_as_parent, class_name: 'ParentChildAssignment', :foreign_key => :parent_id
has_many :parent_child_assignments_as_child, class_name: 'ParentChildAssignment', :foreign_key => :child_id
has_many :children, :through => :parent_child_assignments_as_parent
has_many :parents, :through => :parent_child_assignments_as_child
end
class ParentChildAssignment < ActiveRecord::Base
belongs_to :parent, :class_name => "Person", :foreign_key => :parent_id
belongs_to :child, :class_name => "Person", :foreign_key => :child_id
end