Ruby-on-rails-3 – How to name the foreign key in ROR 3?

How to name foreign keys in RoR?

I used the following command to give the foreign key:

rails generate scaffold Table2 id:integer Table1:references

this The command adds the foreign key of Table1 in Table2, but the default name is Table1_id. So how can I provide a custom name for it, such as my_table_f_key instead of Table1_id.

I am using Ruby 1.9.2 and Rails 3.0.3 .

Edit:-

In my project.rb model:

belongs_to :own, :class_name => User 

In my user.rb model:

has_many :owned_projects, :class_name => Project, :foreign_key => :owner

How I created the project model

rails generate scaffold Project name:string owner:integer

Now when I access user_id from Project< br>project.owner.userid it throws an exception.

According to your response in the comments, This is a way to achieve what you want to do:

Suppose you have two models (user and question) in your application, and two different relationships:

>Users have asked a lot of questions, and the questions belong to Asker
>Users have edited a lot of questions, and the questions belong to the editor

You can implement this structure in the following ways:

rails generate scaffold Question asker_id:integer editor_id:integer

Specifying id: integer in the generate command is redundant, because Rails will automatically generate the column for you. Naming in terms of relationship The foreign key is also regular (i.e. asker_id).

Then, in each model:

class Question  belongs_to :asker, :class_name => User
belongs_to :editor , :class_name => User
end

class User has_many :asked_questions, :class_name => Question, :foreign_key => :asker_id
has_many :edited_questions, :class_name => Question, :foreign_key => :editor_id
end

This way, you can use them like this:

@question.asker # => User
@question.editor # => User

@user.asked_questions # => [Question, Question, Question]
@user. edited_questions # => [Question, Question]

I hope this helps.

How to name foreign keys in RoR?

I used the following command to give the foreign key:

rails generate scaffold Table2 id:integer Table1:references

this The command adds the foreign key of Table1 in Table2, but the default name is Table1_id. So how can I provide a custom name for it, such as my_table_f_key instead of Table1_id.

I am using Ruby 1.9.2 and Rails 3.0.3 .

Edit:-

In my project.rb model:

belongs_to :own, :class_name => User 

In my user.rb model:

has_many :owned_projects, :class_name => Project, :foreign_key => :owner

How I created the project model

rails generate scaffold Project name:string owner:integer

Now when I access user_id from Project< br>project.owner.userid it throws an exception.

According to your response in the comments, this is a way to achieve what you want to do Way:

Assuming that there are two models (user and question) in your application, and two different relationships:

>Users ask a lot of questions, questions Belongs to Asker
>Users have edited a lot of questions, and the questions belong to the editor

You can implement this structure in the following ways:

rails generate scaffold Question asker_id :integer editor_id:integer

Specify id in the generate command: integer is redundant, because Rails will automatically generate the column for you. Naming foreign keys in terms of relations is also conventional (ie asker_id).

p>

Then, in each model:

class Question  be longs_to :asker, :class_name => User
belongs_to :editor, :class_name => User
end

class User has_many :asked_questions, :class_name => Question, :foreign_key => :asker_id
has_many :edited_questions, :class_name => Question, :foreign_key => :editor_id
end

This way, you can look like this Use them:

@question.asker # => User
@question.editor # => User

@user.asked_questions # => [Question, Question, Question]
@user.edited_questions # => [Question, Question]

Hope this helps.

Leave a Comment

Your email address will not be published.