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 779107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:52:29+00:00 2026-05-14T19:52:29+00:00

I am not sure am I doing these correct. I have 3 models, Account,

  • 0

I am not sure am I doing these correct.

I have 3 models, Account, User, and Event.

Account contains a group of Users. Each User have its own username and password for login, but they can access the same Account data under the same Account.

Events is create by a User, which other Users in the same Account can also read or edit it.

I created the following migrations and models.


User migration

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.integer     :account_id
      t.string      :username
      t.string      :password
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Account migration

class CreateAccounts < ActiveRecord::Migration
  def self.up
    create_table :accounts do |t|
      t.string      :name
      t.timestamps
    end
  end

  def self.down
    drop_table :accounts
  end
end

Event migration

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.integer     :account_id
      t.integer     :user_id
      t.string      :name
      t.string      :location
      t.timestamps
    end
  end

  def self.down
    drop_table :events
  end
end

Account model

class Account < ActiveRecord::Base
  has_many      :users
  has_many      :events
end

User model

class User < ActiveRecord::Base
  belongs_to    :account
end

Event model

class Event < ActiveRecord::Base
  belongs_to    :account
  belongs_to    :user
end

so….

  1. Is this setting correct?
  2. Every time when a user create a new account, the system will ask for the user information, e.g. username and password. How can I add them into correct tables?
  3. How can I add a new event?

I am sorry for such a long question. I am not very understand the rails way in handling such data structure. Thank you guys for answering me. 🙂

  • 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-14T19:52:30+00:00Added an answer on May 14, 2026 at 7:52 pm

    This looks like a job for has_many :through (scroll down to find the :through option)

    If you need to know the user that created the event, then you should specify that Event really belongs only to a user:

    class Event < ActiveRecord::Base
      belongs_to    :user
    end
    

    Accounts, however, can “grab” their user’s events. You specify that like this:

    class User < ActiveRecord::Base
      belongs_to :account
    end
    
    class Account < ActiveRecord::Base
      has_many :users
      has_many :events, :through => :users
    end
    

    The migrations would be the same as you wrote for Account and User. For Event you can remove the account_id:

    class CreateEvents < ActiveRecord::Migration
      def self.up
        create_table :events do |t|
          t.integer     :user_id
          t.string      :name
          t.string      :location
          t.timestamps
        end
      end
    
      def self.down
        drop_table :events
      end
    end
    

    Then your events can be created like this:

    # These two are equivalent:
    event = user.events.create(:name => 'foo', :location => 'bar')
    event = Event.create(:user_id => user.id, :name => 'foo', :location => 'bar')
    

    Notice that this will create and save the event inmediately. If you want to create the event without saving it, you can use user.events.build or Event.new instead.

    The has_many :through on Accounts will allow you to get all the events for one account:

    user.events         # returns the events created by one user
    account.events      # returns all the events created by the users of one account
    user.account.events # returns the events for the user's account
    

    As a final note, please note that you are reinventing the wheel a lot here. There are pretty good solutions out there for managing users and permissions.

    I recommend you to have a look at devise (railscast) or authlogic (railscast) for managing your accounts, and declarative_authorization(railscast) or cancan(railscast) for managing permissions. My personal choices are devise and declarative_authorization. The former is easier to install than authlogic, and the later is more powerful than cancan.

    Regards, and good luck!

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

Sidebar

Related Questions

Im not sure if I'm doing this right. I have an action that I
Am not sure if what I am doing is absolutely correct. But here goes:
I have a hashmap that contains 10 children hashmap, each of these child hashmap
I know this probably really simple but Im not sure what im doing wrong...
Not sure what I'm doing wrong with .live() $(function(){ var wrapper = $('#trailer_wrapper'); var
Not sure if I am doing this correctly or not. Here is my .js
Not sure what I'm doing wrong but I can't get my JQuery AJAX call
I am not sure what I should be doing here. Should I be hardcoding
I am not sure what I am doing wrong, It would be great if
I am not sure what I could be doing wrong that causes info level

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.