简体   繁体   中英

relationship belongs_to to belongs_to in ruby on rails

I have a relationship belongs_to to belongs_to between two model in rails. I have the model user and the model Startup

The model User is a devise model (gem devise).

this is my code.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
  # attr_accessible :title, :body

  belongs_to :startup
end

And this is my model Startup

class Startup < ActiveRecord::Base
  attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
  has_one :entrepreneus, dependent: :destroy

  belongs_to :user
end

Also in my schema I have added "user_id" in Startup table.

this is the add_index added

  add_index "startups", ["user_id"], :name => "index_startups_on_user_id"

(Obviusly I did a migration)

When I create a Startup in the first moment create the object without user_id, but with update_attributes add the user_id in the object. (In the startup_controller). This is the code

def create
    @startup = Startup.new(params[:startup])
    @startup.update_attributes(:user_id => current_user.id )

    respond_to do |format|
      if @startup.save
        format.html { redirect_to @startup, notice: 'Startup was successfully created.' }
        format.json { render json: @startup, status: :created, location: @startup }
      else
        format.html { render action: "new" }
        format.json { render json: @startup.errors, status: :unprocessable_entity }
      end
    end
  end

In the console I do the following (rails c --sandbox)

u =  User.first (retrieve the user perfectly, the id is equal to 1)

u.startup (retrieve null)

But if you does this:

s = Startup.find_by_user_id(1) // retrieve the data

Why I can't retrieve data associated to startup with u.startup ?.

Any idea ?. Thanks

PDT: Sorry my english still is not very well.

If it's a one-to-one relationship between two models you have to use has_one on the side that doesn't have the foreign key in the table. So in this case, instead of:

belongs_to :startup

you have to do:

has_one :startup

I'm newer to rails so someone correct me if i'm wrong, but i don't think you can have 2 models that "belong_to" each other. 1 has to have belong to and 1 has to have has_many or has_one (edit: as per comment, it should be :has_one .

I think this may be what you're looking for:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
  # attr_accessible :title, :body

  has_one :startup # not has_many :startups
end

class Startup < ActiveRecord::Base
  attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
  has_one :entrepreneus, dependent: :destroy

  belongs_to :user
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM