Here are my two models
class Answer
include Mongoid::Document
belongs_to :question
has_many :responses
has_many :validations, :dependent => :delete
end
and
class Question
include Mongoid::Document
has_many :answers, :dependent => :delete # it might not always have answers
belongs_to :correct_answer, :class_name => "Answer", :dependent => :delete
end
What is missing here? Not able to resolve it
Related
I have the following relationship. I want a Post to has_one current_wrent but also has_many wrents that keeps tracks of wrent objects. I believe the issue may be related to rails being confused which relationship i'mr eferring to.
When I refer to a post.current_wrent, this is returning correctly with no errors.
Class Post
include Mongoid::Document
include Mongoid::Timestamps
...
has_one :current_wrent, :class_name => "Wrent", :inverse_of => :current_wrent, :autosave => true, :dependent => :destroy
has_many :wrents, :inverse_of => :post, :dependent => :destroy
end
Class Wrent
..
belongs_to :post, :autosave => true
..
end
When I do something like..
(in Wrent.rb)
def accept!
update_attributes!(:status => 1, :accepted => true)
post.current_wrent = self
post.available = false
post.save!
notify_user
end
and I get a "Current Wrent" is invalid error, could somebody point to me something I'm doing wrong here?
EDIT: this seems to work fine.
in wrent.rb
Class Wrent
..
belongs_to :post, :inverse_of => :wrent, :autosave => true
belongs_to :post, :inverse_of => :current_wrent
in post.rb
Class Post
...
has_one :current_wrent, :class_name => "Wrent", :inverse_of => :post
belongs_to :current_wrent, :class_name => "Wrent", :inverse_of => :post
has_many :wrents, :inverse_of => :post, :dependent => :destroy
I'm still not sure what the problem is, but now I can access post.current_wrent through the belongs_to current_wrent_id column and the problem seemed to disappear.
Your Wrent model probably has a post_id field where is stored the id of the Post that it belongs_to. But there's no field in Post to store the current_wrent.
Mongoid allows you to embed objects so what you can do is embeds_one instead has_one.
Class Post
include Mongoid::Document
include Mongoid::Timestamps
...
embeds_one :current_wrent, :class_name => "Wrent", :inverse_of => :current_wrent
has_many :wrents, :inverse_of => :post, :dependent => :destroy
end
I have a User model with a has_many :through relationship to the Publication model. The Publication model in turn has a has_many :through relationship to Author:
class User < ActiveRecord::Base
has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
has_many :publications, :through => :library_publications
end
class Library::Publication < ActiveRecord::Base
belongs_to :publication
belongs_to :user
end
class Publication < PublicationBase
has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
has_many :users, :through => :library_publications
has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
has_many :authors, :through => :publication_contributions
end
class Author < AuthorBase
has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
has_many :publications, :through => :publication_contributions
end
class Publication::Contribution < Publication::ContributionBase
belongs_to :publication, :class_name => "Publication"
belongs_to :author, :class_name => "Author"
end
As far as I can tell, all the associations are written correctly. However, when I try to eagerload authors from a user:
#user.library_publications.includes(:publication => [:authors])
I get this error:
Association named 'authors' was not found; perhaps you misspelled it?
What might be the cause of this?
After experimenting a little, I discovered that all of publication's associations were broken. This led to me to looking for larger problems, and eventually I discovered that this issue was caused by one of the join-table being namespaced, Library::Publication. When I de-namespaced it, publication's associations began working again.
I'm not sure why this happened, though. If anyone has an explanation, please share.
I have 3 tables Collections, Tracks and ProductContributors
Association of them is as follows
class Collection < ActiveRecord::Base
has_many :product_contributors, :as => :product
has_many :tracks, :through => Product_contributors, :as=> :product
end
class Track < ActiveRecord::Base
has_many :product_contributors, :as => :product
has_many :collections, :through => Product_contributors, :as => :product
end
class ProductContributor < < ActiveRecord::Base
belongs_to :product, :polymorphic => true
belongs_to :collection
belongs_to :track
end
whenever i hit the url for product contributor i get the following error :
Expected /app/models/track.rb to define TRACK
I've gone through this url but didnt help me in any case. I dont have the autoload issue, all my models are loaded properl
Any help would be highly appreciated..!!
I dare say its because of the typo in your Track class.
has_many :collections, :through => Product_contributors, :as => :product
is not valid. Try:
has_many :collections, :through => :product_contributors, :as => :product
Basically, it is trying to load the Model, but its finding the typo in the association, and it is then not loading, causing it to seem like the class is not there. I assume you will have a similar situation with the Collection class as well.
My has_many :through associations on Releases/Products/Tracks seem to be deleting the the Track and leaving orphaned associations in the releases_tracks / products_tracks tables. I can't see where i've gone wrong, I thought the default behavior is to delete the association only. Can anyone help please?
My Models:
class Track < ActiveRecord::Base
has_many :releases_tracks
has_many :tracks, :through => :releases_tracks
has_many :products_tracks
has_many :products, :through => :products_tracks
end
class Release < ActiveRecord::Base
has_many :releases_tracks
has_many :tracks, :through => :releases_tracks
end
class Product < ActiveRecord::Base
has_many :products_tracks
has_many :tracks, :through => :products_tracks
before_save do
self.track_ids = self.releases_track_ids
end
end
class ProductsTrack < ActiveRecord::Base
belongs_to :product
belongs_to :track
end
class ReleasesTrack < ActiveRecord::Base
belongs_to :release
belongs_to :track
end
My Track Controller (for the destroy action):
class TracksController < ApplicationController
before_filter :get_track_parent
def destroy
#track = #parent.tracks.find(params[:id])
#track.destroy
redirect_to #parent
end
private
def get_track_parent
if params[:product_id].present?
#parent = Product.find(params[:product_id])
elsif params[:release_id].present?
#parent = Release.find(params[:release_id])
end
end
end
My destroy link in the releases view:
<%= link_to image_tag("icons/delete.png"), release_track_path(#release,track), :confirm => 'Are you sure?', :method => :delete %>
And finally, my destroy link in the products view:
<%= link_to image_tag("icons/delete.png"), product_track_path(#product,track), :confirm => 'Are you sure?', :method => :delete %>
First of all, you need :dependent => :destroy option for your associations:
class Track < ActiveRecord::Base
has_many :releases_tracks, :dependent => :destroy
has_many :releases, :through => :releases_tracks # also note you had here :tracks instead of :releases
has_many :products_tracks, :dependent => :destroy
has_many :products, :through => :products_tracks
end
And also if you want tracks to be removed when removing releases or products, add following :dependent => :destroys:
class Release < ActiveRecord::Base
has_many :releases_tracks, :dependent => :destroy
has_many :tracks, :through => :releases_tracks
end
class Product < ActiveRecord::Base
has_many :products_tracks, :dependent => :destroy
has_many :tracks, :through => :products_tracks
before_save do
self.track_ids = self.releases_track_ids
end
end
class ProductsTrack < ActiveRecord::Base
belongs_to :product
belongs_to :track, :dependent => :destroy
end
class ReleasesTrack < ActiveRecord::Base
belongs_to :release
belongs_to :track, :dependent => :destroy
end
Ok, I am trying to display the profile pic of a user. The application I have set up allows users to create questions and answers (I am calling answers 'sites' in the code) the view in which I am trying to do so is in the /views/questions/show.html.erb file. It might also be of note that I am using the Paperclip gem. Here is the set up:
Associations
Users
class User < ActiveRecord::Base
has_many :questions, :dependent => :destroy
has_many :sites, :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :likes, :through => :sites , :dependent => :destroy
has_many :pics, :dependent => :destroy
has_many :likes, :dependent => :destroy
end
Questions
class Question < ActiveRecord::Base
has_many :sites, :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :likes, :dependent => :destroy
belongs_to :user
end
Answers (sites)
class Site < ActiveRecord::Base
belongs_to :question
belongs_to :user
has_many :notes, :dependent => :destroy
has_many :likes, :dependent => :destroy
has_attached_file :photo, :styles => { :small => "250x250>" }
end
Pics
class Pic < ActiveRecord::Base
has_attached_file :profile_pic, :styles => { :small => "100x100" }
belongs_to :user
end
The /views/questions/show.html.erb is rendering the partial /views/sites/_site.html.erb which is calling the Answer (site) with:
<% div_for site do %>
<%=h site.description %>
<% end %>
I have been trying to do things like:
<%=image_tag site.user.pic.profile_pic.url(:small) %>
<%=image_tag site.user.profile_pic.url(:small) %>
etc. But that is obviously wrong. My error directs me to the Questions#show action so I am imagining that I need to define something in there but not sure what. Is is possible to call the pic given the current associations, placement of the call, and if so what Controller additions do I need to make, and what line of code will call the pic?
UPDATE: Here is the QuestionsController#show code:
def show
#question = Question.find(params[:id])
#sites = #question.sites.all(:select => "sites.*, SUM(likes.like) as like_total",
:joins => "LEFT JOIN likes AS likes ON likes.site_id = sites.id",
:group => "sites.id",
:order => "like_total DESC")
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #question }
end
end
You have has_many association for the pics:
class User < ActiveRecord::Base
...
has_many :pics, :dependent => :destroy
end
But you are trying to act, as if there was only one pic:
<%=image_tag site.user.pic.profile_pic.url(:small) %>
So either take first picture (probably you should also check if it exists) :
<%=image_tag site.user.pics.first.profile_pic.url(:small) %>
Or change the association to has_one if user can have only one picture:
class User < ActiveRecord::Base
...
has_one :pic, :dependent => :destroy
end
<%=image_tag site.user.pic.profile_pic.url(:small) %>