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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:32:17+00:00 2026-05-26T08:32:17+00:00

I have projects and users joined via has_many :through join model called ownerships. Each

  • 0

I have projects and users joined via has_many :through join model called ownerships. Each ownership has an owner_type attribute that I want to access.

I want to have a way to pass a user_id to a project model and get the owner_type via the ownership join model. And visa versa I want to pass a project_id to a user model and get the owner_type via the ownership join model. Here is what I have:

class Project < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :ownerships
  has_many :users, :through => :ownerships

  validates :name, :presence => true,
                   :length => { :maximum => 50 }

  def owner_type?(user_id)
    @user_id = user_id
    @ownership = self.ownerships.where(:user_id => @user_id)
    @ownership.owner_type
  end

end

class User < ActiveRecord::Base
  attr_accessible :name, :email, :admin

  has_many :ownerships
  has_many :projects, :through => :ownerships

  accepts_nested_attributes_for :projects

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true,
                   :length => { :maximum => 50 }

  #validates :email, :presence => true,
  #                  :format => { :with => email_regex },
  #                  :uniqueness => { :case_sensitive => false }

  validates_inclusion_of :admin, :in => [true, false]

  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["user_info"]["name"]
      user.admin = false
    end
  end
end

class Ownership < ActiveRecord::Base
  attr_accessible :owner_type

  belongs_to :project
  belongs_to :user

  validates :user_id, :presence => true

  validates :project_id, :presence => true

  validates :owner_type, :presence => true

end

and then in my project#show page:

<%= @project.owner_type?(current_user.id) %>

what am I doing wrong? How do I access the owner_type attribute from the ownership model from anywhere? It never works.

  • 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-26T08:32:18+00:00Added an answer on May 26, 2026 at 8:32 am

    You haven’t pasted the actual error you are getting. But here is what I think is going wrong.

    ownerships is a has_many relationship. It will return you a collection of results, always. Thus, @ownership = self.ownerships.where(:user_id => @user_id) will return you an array. So you should probably be doing if you expect only 1 result.

    @ownership = ownerships.where(:user_id => @user_id).first
    @ownership.owner_type
    

    This should work.

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

Sidebar

Related Questions

I have an application which has a Projects table and a Users table. In
I have an app that has Basecamp-style subdomains, that is, I have Projects, Users,
I have apps Users and Projects and would like to define another app called
I have the following 2 models: Projects, has_many projects Users belong_to Projects @project =
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have a projects table which has two foreign keys for users (user_id and
When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
I have the following model setup - a User is interested in projects in
I have tables: users {id, name} projects {id, name} roles {id, name} projects_users {id,
I have an application that requires: user owns many projects. project has one owner.

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.