I hope someone here can help me. I am getting the following error:
uninitialized constant Subscription::SubscriptionContactDatum
The problem is that SubscriptionContactDatum doesn't exist. I have a class called SubscriptionContactData, but not SubscriptionContactDatum. After some days wondering what's going on I found that the problem came from the dependencies.
There are two classes related: Subscription and SubscriptionContactData. Subscription can has many ContactData and each ContactData belongs to 2 different Subscriptions: Subs_cont_datas(id, subscription_id1, subscription_id2)
I though the problem was there, so I tried adding a second :belongs_to to ContacDatas with no luck.
subscription.rb
class Subscription < ActiveRecord::Base
has_many :subscription_contact_data, :dependent => :destroy
end
subscription_contact_data.rb
class SubscriptionContactData < ActiveRecord::Base
belongs_to :subscription
end
Note 1: If I delete the :dependent => :destroy fon Subscriptions.rb the error disappears, but the DB throws a foreing key constraint error (obviously). So deleting the :dependent is not an option...
Note 2: I’m using Ruby 1.8.9, Rails 3.0.1
I think, problem is not with :dependent => destroy. It is the Problem with "rails trying to pluralize the association name". So, you can try the below line.
has_many :subscription_contact_data, :class_name => 'SubscriptionContactData'
This should solve your issue.
Hope it helps.
Related
Given two associated models in rails (4.0),
class User < ActiveRecord::Base
has_one :subscription, dependent: :destroy
end
class Subscription < ActiveRecord::Base
belongs_to :user
end
The above code will ensure that when an instance of User is destroyed, its associated record will be, too.
So far so good.
My question is, is it possible to equally easily invoke a dependent update as well, so that every time User is updated, Subscription will be updated as well?
This could look like this:
class User < ActiveRecord::Base
has_one :subscription, dependent: [:update, :destroy]
end
So that when User gets updated successfully, the associated Subscription will re-save, thus invoking its update filters (i.e. before_save, before_update, after_save, after_update).
Is there an elegant way to do this? If not, what is the closest way to cleanly get to this?
Thank you!
Try this,
has_one :subscription, :dependent => destroy, :autosave => true
For more details see the documentation http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html
I have a many to many :through relationship between a set of classes like so:
class Company
has_many :shares
has_many :users, :through => :shares, :uniq => true
end
class User
has_many :shares
has_many :companys, :through => :shares, uniq => true
end
class Share
belongs_to :company
belongs_to :user
end
I want to ensure a unique relationship so that a user can only have one share in any one company, which is what I have tried to achieve using the "uniq" argument.
At first I thought this was working, however it seems the behaviour os the "uniq" is to filter on the SELECT of the record, not pre-INSERT so I still get duplicate records in the database, which becomes an issue if I want to start dealing with the :shares association directly, as calling user.shares will return duplicate records if they exist.
Can anyone help with an approach which would force truely uniq relationships? so that if I try adding the second relationships between a user and a company it will reject it and only keep the original?
Have you tried adding this to your Share class?
validates_uniqueness_of :user, scope: :company
Also, in your User class I think it should be:
has_many :companies, through: :shares
I hope that helps.
I have three models:
Class Project < ActiveRecord::Base
has_many :tasks
has_many :tags, :through => :tasks
end
Class Tasks < ActiveRecord::Base
belongs_to :project
has_and_belongs_to_many :tags
end
Class Tags < ActiveRecord::Base
has_and_belongs_to_many :tasks
has_many :projects, :through => :tasks
When I open up console, I can get my Project and Task information as expected:
Tag.find(1).projects
Tag.find(1).tasks
If I want, I can get all the tasks for each project regardless of the tag:
Project.find(1).tasks
For whatever reason, I can't access tasks if I get projects by tag...
something = Tag.find(1).projects
something.tasks
...I get the error:
undefined method `tasks' for #<ActiveRecord::Relation:0x007feae4af0e70>
I've looked for a couple hours and can't find anything that corrects this problem. Based on everything I've found, it should be working... but it's not.
I'm using Rails 3.2.3.
Shouldn't Tag.find(1).tasks give you the same result?
Anyway, the problem you're facing is that you're trying to retrieve an association from a Relation object instead of an instance of your model. Relations can be used to chain query conditions, but you can't directly reference associations from them. So, to get your example working, you'd need to do
p = Tag.find(1).projects.includes(:tasks)
Then reference tasks like this: p[0].tasks.
However I'd just make sure that Tag.find(1).tasks will generate the same SQL and ultimately return the same collection of tasks.
I print in my view a number that tell me, how many people read my article. It looks something like a:
<%=article.hits.count%>
As is possible to see, I created a simple association.
Now I am trying to get the information, if the user who is log in on my page, so if he is already had read this article. In my table that contains hits is column user_id.
But I can't still find the way, how to get...
I tried something like:
<% if session[:login_user_id].hits.user_id == session[:login_user_id]%>
Have you read it already.
<% end %>
But the example above doesn't work me... Could anyone help me please, how to do?
EDIT: The models:
class Article < ActiveRecord::Base
has_many :hits
end
class Hits < ActiveRecord::Base
belongs_to :article, :class_name => "DataHit", :foreign_key => "article_id"
has_many :users
end
class User < ActiveRecord::Base
belongs_to :hit
end
Thanks in advance
Let's first talk about the model you like to receive. For me, it sounds like:
Every article can be visited / read by many users.
Every user can read / visit many articles.
This is a classical n:m-association which is normally implemented by a has-many-through association.
If this is the intention, it should be implemented like:
class Article < ActiveRecord::Base
has_many :hits
has_many :users, :through => :hits
end
class Hits < ActiveRecord::Base
belongs_to :article, :class_name => "DataHit", :foreign_key => "article_id"
belongs_to :user
end
class User < ActiveRecord::Base
has_many :hits
has_many :articles, :through => :hits
end
Of course, you have to add migrations that ensure that the final DB model is like that:
Hit has article_id and user_id to ensure that users may find the articles they have read
If you have that model implemented, it should be more easy. Then you have operations available like: #article.users.contains(User.find(user_id)). Have a look at the tutorial at Ruby on Rails Guides which explain what the has-many-through relation is and which advantages they have.
It would be helpful if you try the things first in the console of Rails. To do that, start with:
Start the rails console in the root directory of your application: rails c
Enter there e.g.: art = Article.find(1) to get the article with the id.
Try which methods are available: art.methods.sort to see all methods that could be used. If there is no method users, you have did something wrong with the assocication.
Try the call: us = art.users and look at the result. It should be a rails specific object, an object that behaves like a collection and understands how to add and remove users to that collection (with the whole life cycle of rails). The error your currently have could mean different things:
Your database model does not match your associations defined in Rails (I suspect that).
Some minor tweak (misspelling somewhere) which hinders Rails.
I hope this gives you some clues what to do next, I don't think that we can fix the problem here once and for all times.
I have my models setup like so:
class User < ActiveRecord::Base
has_many :posts, :foreign_key => 'author_id'
end
class Post < ActiveRecord::Base
belongs_to :author, :class_name => 'User'
end
Assuming:
p = Post.first # just any post instance
a = User.first # any user instance
Now this piece of code is acting very weird
p.author = a
After setting the author, the attribute author_id of the post should be set to the id of the user. But this isn't happening.
I tried using models with belongs_to that does not have the class_name parameter and everything works as expected.
Now, one more thing that makes it weirder is that when I change the association to belongs_to :author, :class_name => 'User', :foreign_key => 'author_id', it surprisingly works.
Is this a bug in Rails 3.0.9? Shouldn't the foreign key parameter be unnecessary because as the docs say, its default value is the name of the association appended with _id.
Also note that even without :foreign_key => 'author_id', everything else regarding the association works as expected. (Like fetching the associated model) The only thing not working is the setter method not setting the foreign key.
I know I could just do p.author_id = a.id or just add :foreign_key params to all my associations with class_name, but I prefer the more elegant syntax of p.author = a
After reading through lots of Rails code and tracing here's what I found:
This bug exists because of the gem composite_primary_keys which overrode the default rails reflection.rb.
I will have to check how they implemented the primary_key_name and derive_primary_key_name methods.
It was quite a bit of time wasted on this silly bug, but at least I got to learn a lot about ActiveRecord's internals.