Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:11:40+00:00 2026-05-25T23:11:40+00:00

I have the models User and StoredItem: class UserData < ActiveRecord::Base has_many :stored_items, :dependent

  • 0

I have the models User and StoredItem:

class UserData < ActiveRecord::Base
  has_many :stored_items, :dependent => :destroy
end

class StoredItem < ActiveRecord::Base
  belongs_to :user
  named_scope :lookup, lambda { |id| { :conditions => ['qid = ?', id]}}
end

I need to have two methods to add and remove the items to StoredItem for current user. I put this code to User model:

class UserData < ActiveRecord::Base
  has_many :stored_items, :dependent => :destroy

  def save_item(params)
    if(!self.stored_items.lookup(params[:qid]).exists?)
      item = self.stored_items.new(:sid => params[:qid],
                                   :name => params[:qti],
                                   :url => params[:qur],
                                   :group_id => params[:title],
                                   :rating => Integer(params[:rating]))
      item.save
    end
  end

  def remove_item(qid)
    item = self.stored_items.lookup(qid).first()
    item.destroy
  end
end

So here is the StoredItem controller:

def save_item
  @user = UserData.find_by_login(session[:cuser])
  @user.save_item(params)
  # ...
end

Is it good architectural decision or it will be better to put this code to StoredItem model and pass the current user into it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:11:41+00:00Added an answer on May 25, 2026 at 11:11 pm

    This is a good architectural decision. You need to keep it in the user since the User is the owner of the StoredItem. The user is responsible for its stored items, not the other way around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have models: class User < ActiveRecord::Base has_many :user_skills end class UserSkill < ActiveRecord::Base
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have 3 models class User < ... belongs_to :language has_many :posts end class
I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent =>
I have two models class User < ActiveRecord::Base has_many :requests, :foreign_key => :recipient_id .
I have two models: User and Store class Store < ActiveRecord::Base belongs_to :user class
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have 3 models: User has_many :questions has_many :corrections end Question has_one :correction belongs_to
I have 3 models sites, user_favorites and users. Relevant relationships: class Site < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.