in a Rails 3 application there are two models assinged to each other by belongs_to and has_one. On both sides there is :depended => :destroy configured for this association.
now I had to add a :before_destroy callback in one of these models. the problem is now that this callback is triggered twice when an entity which includes this callback is destroyed. When I remove :depended => :destroy in the other model, it's triggered only once. So it seems this i causing the problem.
is there an elegant way to fix this ?
:dependent
Controls what happens to the associated objects when their owner is destroyed
dependent destroy must be alone in a model
has_many :comments, dependent: :destroy
has_one :position, dependent: :destroy
Sorry for my english XD
Related
I am trying to learn to model SQL databases, and am having some trouble understanding some concepts. I want to a build an app, where users can split a bill at a restaurant. Could someone please tell me if the following is allowed?
I have Bills, Items, and Users
Bill has_many :items, dependent: :destroy
Bill has_many :users, foreign_key: :user_id
User has_many :bills, foreign_key: :created_by
Item belongs_to :bill
Is it ok for a user to have many bills, and a bill to have many users?
This is a more fundamental database design issue. If I were to design this database for a bill sharing app, it would be like this
user.rb
has_many :bill_contributions
has_many :bills, through: :bill_contributions
bill.rb
has_many :items
has_many :bill_contributions
has_many :users, through: :bill_contributions
bill_contribution.rb
belongs_to :user
belongs_to :bill
I split it like this because there is a has many and belongs to many relationship between the user and bills as a user can have a lot of bills and bills can be split among many users. you can look up HABTM
database relationships if you wish to learn more
i have an application am designing which consists of users making friends and users
having many posts, paintings, friends and talks
the model is below
class User < ActiveRecord::Base
has_many :paintings, :dependent => :destroy
has_many :sells, :dependent => :destroy
has_many :posts, :dependent => :destroy
has_many :talks, :dependent => :destroy
has_many :friends
has_many :comments
end
i have set a notification system in which when a user creates a post or painting e.t.c
a notification is sent to the users friends which i have achieved with public_activity gem,
what i intend to achieve is a notification system in which when the notification is created, every user involved can mark as seen i.e they have seen it so THAT notification is not shown to the user anymore... And also when a user comments on a notification, i want a notification to be sent to the owner of the activity, and any other user that comments on THAT notification... IN SUMMARY, I NEED A FACEBOOK LIKE NOTIFACATION SYSTEM...
Have you considered creating a Notification after the relevant post/painting has been added?
Notifications would be created (probably using an after_create action on each of the relevant models e.g. posts, paintings).
class Post < ActiveRecord::Base
after_create :create_notification
private
def create_notification
# create notification here
end
end
If there are lots of notifications to be created then you may wish to consider creating them in a background job.
A notification would belong to the target friend of the user and could therefore be marked as seen through an attribute on the notification. You would include instance methods on the notification object to deal with this.
class Notification < ActiveRecord::Base
belongs_to :user # friend of the originating user
def mark_seen
update_attributes(viewed: true)
end
end
You could also add scopes on the notifications to make sure that users can easily see unseen notifications.
class User < ActiveRecord::Base
has_many :notifications
has_many :unseen_notifications, conditions: "notifications.viewed IS false" #or something like that
end
Hope this helps to set you on the right track.
I am using Rails 4.0 and would like to use Devise for my sign-up/sign-out/etc.
However, on my sign-up page I would like to have fields not only from the user model that devise creates but also another model that users have a relationship with (organizations in this case).
I have setup associations on the models for user and organization like so:
user.rb
has_one :organization
accepts_nested_attributes_for :organization
organization.rb
belongs_to :user
Any ideas on how to make this happen? I tried overriding the registration controller for devise but haven't had any luck.
You need to override the views and insert a fields_for block for that :organization association. Run rails generate devise:views and look for the new view files in your app/views/devise directory.
I have just added devise_invitable to an app with a working implementation of devise already in place.
The invitation process itself all works fine (email is sent, new user can click link and set password etc).
The problem is that the inviter, a User, has an associated Profile, which is deleted when the inviter hits the 'Send an invitation' button.
Anyone have any idea why the invitation process would nuke an associated object on the inviter? I am going to try to trace this through the devise_invitable code, but it would be good to know if anyone has had this problem before, or knows where in devise's code the problem might lie.
The User:
has_one :profile, :inverse_of => :user, :dependent => :destroy
The Profile
belongs_to :user, :inverse_of => :profile
The error here is because – by default – devise_invitable's after_invite_path_for simply calls after_sign_in_path_for.
However, it does not pass the current user as the resource, which would be the case with a regular sign in.
If you override after_sign_in_path_for assuming it will only be called after a genuine sign in, as I did, this can be confusing.
(In my particular case: what my overridden method did was to look for the resource's associated profile, and if it didn't have one it would make a new one assuming the user is logging in for the first time. When devise_invitable passed an unexpected resource, this new profile object would overwrite the existing association and the :dependent => :destroy callback would be triggered on the old profile object. D'oh!)
I am designing a website which has many users, each user has many posts, and each post has many photos. Because I hope user can preview the upload photos when they are creating new post, I found the answer here, which suggested using a subform to upload photos.
I use carrierwaves to handle photo upload, and jquery-file-upload for user interface. The models are:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
attr_accessible :title, :description, :photo_ids
belongs_to :user
has_many :photos, :dependent => :destroy
end
class Photo < ActiveRecord::Base
mount_uploader :image, ImageUploader
attr_accessible :image
belongs_to :post
end
In the new post view, I put a jquery-file-upload UI above the normal post form, then:
When user select a photo, it will be uploaded to PhotosController via jquery-file-upload api. The post_id for this photo is nil at this step.
After photo uploaded finished, I use javascript to add a hidden input form like this:
input type="hidden" id="collection_image_ids" name="collection[collection_image_ids][]" value=#{the id of the photo}
User can repeat step 1&2 many times, then submit the post.
Everything works well, however....
I don't think it's a good idea to allow mass assignment for photo_ids because someone can use this request to assign other user's photo to his post. (in edit view)
The reference answer suggested using some randomized access key to improve the security,
but when someone visit other user's post, he can still get the randomized access key of those photos, right?
So the implementation is not safe for now right?
Could anyone give me some suggestions or what is the proper way to handle this problem?