Question and answer site for professional and enthusiast programmers

Q&A programmers.

Rails Building an Object that has a Parent? – ruby-on-rails, ruby-on-rails-3



I have the following two models in my app Spaces which are like projects & Permissions

class Space < ActiveRecord::Base
 has_many :permissions
 has_many :users, :through => :permissions

class Permission < ActiveRecord::Base
  belongs_to :user 
  belongs_to :space
  belongs_to :role
  validates_presence_of :space_id , :user_id, :role_id

So here’s what I’d like to do, in the Space Controller, when the user does DEF CREATE, I want to create a space, and a permission with role_id = 1, to ensure the space/project as the creator as a member…

Here’s what I have. It’s getting messy and not creating the permission

Space Controller:

def create
  @space = current_user.spaces.new(params[:space].merge(:creator => current_user.id))
  @space.permissions.new(:role_id => 1, :user_id => current_user.id)
  respond_to do |format|
    if @space.save
    ..
    ..

Thanks for your help








  • ruby – Rails 3, has_one / has_many with lambda condition – ruby-on-rails, ruby, ruby-on-rails-3, activerecord, lambda
  • Here my models: [code]class User < ActiveRecord::Base has_many :bookmarks end class Topic
  • ruby on rails – Active Record Relation with Postgres and Devise not working – ruby-on-rails, activerecord, devise, relationship, has-many
  • I'm trying to give a User Deeds. [code]class User
  • ruby on rails – ActiveRecord::RecordNotFound: Couldn't find Debtor with id= – ruby-on-rails, ruby, postgresql, rspec, mongoid
  • I recently converted a project from MongoDB/mongoid to PostgreSQL.
  • ruby on rails – Sorting Based on a has_many relationship – ruby-on-rails, ruby, sorting
  • Project is located at and I have a resource review site with
  • URL generation error with Rails 4 in a nested form – forms, url, ruby-on-rails-4
  • I have 2 resources: [code]# config/routes.rb resources :matches do resources :comments,
  • ruby – Rails deployment to Amazon EC2 – No space left on device – ruby-on-rails, ruby, amazon-web-services, amazon-s3, amazon-ec2
  • I am deploying my app to Amazon EC2 with using
  • ruby – Delete object in a has_many through relationship with :dependent => :destroy Rails 3.2 – ruby-on-rails, ruby, database, activerecord
  • Im working on a Rails app where users can create

    Comments are currently closed.

    WP Socializer Aakash Web