has_many through issue with rails 3.2 and acts_as_paranoid - ruby-on-rails-3

I am facing a weird behaviour. This is my scenario.
class Message < ActiveRecord::Base
belongs_to :user, :foreign_key => "from_user_id"
has_many :message_recipients, :include => [:user], :dependent => :destroy
has_many :recipients, :through => :message_recipients, :source => :user
end
class MessageRecipient < ActiveRecord::Base
belongs_to :user
belongs_to :message, :include => :message_recipients
end
class User < ActiveRecord::Base
acts_as_paranoid
has_many :message_recipients
end
I am creating a new message and pushing value to its recipients.
#message=Message.new(:body => "Hi",:from_user_id => session[:user])
#message.recipients.push(User.find(params[:message_recipient_id]))
#message.save
The above operation saves the record in message correctly but fails to trigger the message_recipient record. But if i perform the above code removing the acts_as_paranoid gem, then it works fine. Is there any work around to solve this issue?
Solution 1:
Instead of calling new on Message model we can use create on Message model. So the record will be created and then i can push the data inside the recipients.Its like creating parent record and using its id i am creating child record. So there is no need for trigerring and it works fine.
Suggestions are welcome.

It works fine with rails 3.2.12 but fails with rails 3.2.13. I am confused with this behaviour.

Related

RailsAdmin has_many direct creation

I have a simple has_many attachments situation:
class Project < ActiveRecord::Base
has_many :images, :class_name => 'ProjectImage', :dependent => :destroy
class ProjectImage < ActiveRecord::Base
has_attached_file :image
belongs_to :project
Is it possible (via Rails Admin) to directly add images when creating/editting project?
Now there are two ways (both suck!):
1) Create/Edit a ProjectImage instance and add it to the project (you have to search for it).
2) Add a new Project image which creates a modal and is afterwards same as 1)
The key is: nested attributes using accepts_nested_attributes_for.
eg:
has_many :images, :class_name => 'ProjectImage', :dependent => :destroy, :inverse_of => :project
accepts_nested_attributes_for :images, :allow_destroy => true

RSpec do I test both sides of a relationship

lets say I have two models
class User < ActiveRecord::Base
has_many :friendships, :dependent => :destroy
has_many :followings, :through => :friendships, :foreign_key => "followed_id"
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :following, :class_name => "User", :foreign_key => "followed_id"
end
now in my user_spec.rb I have this test
it "should delete all friendships after user gets destroyed" do
#user.destroy
[#friendship].each do |friendship|
lambda do
Friendship.find(friendship)
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
is this the right place to test the :dependent => :destroy relation or does this belong inside the friendship_spec.rb or doesn't it matter in which of the two specs I test this?
You might consider using shoulda_matchers to test your associations:
# user_spec.rb
it { should have_many(:friendships).dependent(:destroy) }
# friendship_spec.rb
it { should belong_to(:user) }
Having each model test its own associations is the best approach IMHO.
This sort of thing is sometimes a matter of taste, but I think the spec for User is probably the best place to test this. The method you're calling to start the test is a method on User, so it makes sense to test it along the other tests for User as well.

Rails Association (belongs_to) dilemma

I have a User model:
class User < ActiveRecord::Base
has_many :cards
end
and a Card model:
class Card< ActiveRecord::Base
belongs_to :user, :foreign_key => "owner_id"
end
the card model also has an attribute called "owner_id", which I'd like to use in way like this:
Card.first.owner which will retrieve the User which owns that card
my problem as that, I know that rails will automagically connect the id's in the association but that doesnt happen.
in the CardController, rails get stuck in the create action on the line
#card=current_user.cards.new(params[:card])
and says unknown attribute: user_id
I've done db:migrate and it still won't work.
must I do as follows for it to work?
#card = Card.new(params[:card])
#card.owner_id=current_user.id
or am I missing something?
First of all, you don't need a owner_id column for this. All you need is
class User
has_many :cards
end
This will give you #user.cards
class Card
belongs_to :owner, :class_name => "User", :foreign_key => "user_id"
end
This will give you #card.owner

Rails 3 - Nested Form and has_many through association

Hi people, long time not been here. But I'm back because I need your help again please. I have a rails 3.0.9 app, and I'm working with nested forms and has_many through association. When i create an instance, it works great. The problems come when I try to edit. Here is an example for a better explanation. (table names and attributes are just for explaining)
Table Client
id
company_name
address
Table Worker
id
first_name
last name
Table Contact
id
client_id
worker_id
my models looks like these
class Worker < ActiveRecord::Base
has_many :contacts, :dependent => :destroy
has_many :clients, :through => :contacts, :foreign_key => 'client_id'
end
class Client < ActiveRecord::Base
has_many :contacts, :foreign_key => "client_id",:dependent => :destroy
has_many :workers, :through => :contacts, :foreign_key => 'worker_id'
accepts_nested_attributes_for :workers, :allow_destroy => false
end
class Contact < ActiveRecord::Base
belongs_to :worker, :foreign_key => "worker_id"
belongs_to :client, :foreign_key => "client_id"
end
Then in my form for create a client, I can create many workers, and rails make the association and creates the instances for the contacts table (by using nested forms).
The thing is, if I want to edit a client by removing a contact, the contact is not removed. As you can see I put this line in the clients model
accepts_nested_attributes_for :workers, :allow_destroy => false
I set the allow_destroy to false, because I don't want to delete the worker itself, I just want to remove the contact tuple.
Does anybody know how can I solve this?? Hope you can help me... Thanks

Rails 3 - Model Association Problems

I have a project where an User can own a Project and make part of a Project as a Team.
My models are like that:
class User
has_many :projects, :foreign_key => "owner_id"
has_many :project_memberships, :foreign_key => "member_id"
has_many :shared_projects, :class_name => "Project", :through => :project_memberships, :foreign_key => "member_id"
end
class Project
belongs_to :owner, :class_name => "User"
has_many :project_memberships
has_many :members, :class_name => "User", :through => "project_memberships", :foreign_key => "member_id"
end
My question is: How can I create/delete etc a new Project so an User can own it since I'm not using nested resources?
Here is my Project Controller:
def new
#project = Project.new
end
def create
#owner = User.find(params[:user_id])
#project= #owner.projects.build(params[:project])
...
end
Thanks in advance.
If I understand your question correctly, you need to store current signed in user ID in session or use some authentication gem (like devise) which will do it for you.
Devise provides helper method current_user which returns an instance of User model. So you could do like so:
def create
#project= current_user.projects.build(params[:project])
...
end
Update
If you pass user_id through form, you allow anyone to create project with another user's id. Actions that create something, that belong to current user should be constrained to current user on the serverside