How do I relate two fields with Ruby on Rails? - ruby-on-rails

Just starting to use Ruby on Rails to see what its like.
I have a user model with an id and a post model with an adderId. The adderId of the post model should be the user id of the user that created it.
How can I relate these with Ruby on Rails?

The Rails convention for foreign keys would give your Post model a user_id column rather than adderId. You can break the convention, but that requires slightly more configuration, as below:
class User < ActiveRecord::Base
has_many :posts, :foreign_key => :adderId
end
class Post < ActiveRecord::Base
belongs_to :adder, :class_name => "User", :foreign_key => :adderId
end
If you gave Post a user_id instead, you could do this:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
I'd recommend taking a look at the Rails Guide for Active Record Associations, which covers all of the above and plenty more.

It looks like this question seems to be doing what you're trying to do.
Sorry if I misunderstood your question, I too am new to Ruby on Rails.

Related

Associations in ruby on rails

I have created a user using device authentication. I also have created article view, controller and model where model has fields such as title, body and article_id. Now I want to implement comments (with the condition that only logged in user can comment on an article). I have created an is_admin as a special user that has power create new articles through application.
The User table has all fields that are default generated by device. The Article table has fields like article_id, title, and body. There is still no association between user and article table. The Comment table will have (according to my understanding) comment_id, and comment_body.
Expected Associations:
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :user
I want to make sure that only logged in users can comment on articles, and that is_admin user can create new articles.
How can I create association between user, article and comment tables? Do I need to create association for user and comment table?
It is recommended to go through the rails guide and small blog to understand association as #dpassage suggested in comment. Looks you already have work around, so let me consolidate.
As you described, you will have three model:
First: User
class User
has_many :articles # not dependent => :destroy, may you not want to destroy article on deletion of user
has_many :comments, :dependent => :destroy
end
Second: Article
class Article
has_many :comments, :dependent => :destroy
belongs_to :user
end
Third: Comment
class Comment
belongs_to :user
belongs_to :article
end

New to Ruby on Rails - model/migration clarification?

Sorry for the basic, basic question but I'm having some trouble understanding the RoR doc.
say I have 3 models--Students, Classes, and Enrolled_in. If it isn't clear, students will enroll in classes, so Enrolled_in should have Students and Classes as foreign keys. I generated the models for each of these, but I'm confused what I should put into the associated migration file vs. the associated model file for each table. Do I specify the columns of the table in the migration file, and the key constraints in models?
If someone could clarify this, or tell me how they would solve the example question I posted, that would be really helpful. Thanks.
Firstly, to follow the convention the model name should be EnrolledIn, not Enrolled_in. Better yet, change it to Enrollment or something that has a definite meaning as a noun. And you're also going to run into trouble trying to use Class (which is already a ruby object) as a model name. Perhaps change it to Course or something similar.
That aside, you should define all columns in your migrations. You can define the enrolled_ins table like this:
add_table :enrolled_ins do |t|
t.references :student
t.references :course # I'm using course instead of class as noted above
end
The references shortcut will add a :student_id and :course_id as integer fields.
In your model files you'll have:
# student.rb
class Student < ActiveRecord::Base
has_many :enrolled_ins
has_many :courses, :through => :enrolled_ins
end
# course.rb
class Course < ActiveRecord::Base
has_many :enrolled_ins
has_many :students, :through => :enrolled_ins
end
# enrolled_in.rb
class EnrolledIn < ActiveRecord::Base
belongs_to :student
belongs_to :course
end

Rails - Polymorphic association join table

I am currently trying to set up a model structure that seems quite simple, but I haven't quite got it down.
I have a model payment that can belong to either a customer or a supplier (which can both have many payments).
My question is simply whether I need to manually create an interface table to allow this, or if declaring the polymorphic associations will do this for me?
e.g. I have:
class Payment < ActiveRecord::Base
belongs_to :payment_originator, :polymorphic => true
end
class Customer < ActiveRecord::Base
has_many :payments, :as => :payment_originator
end
class Supplier < ActiveRecord::Base
has_many :payments, :as => :payment_originator
end
Is this enough, or do I also need to use a generator to manually create the payment_originator model?
Thanks!
As far as the models go, this is good enough. You just need to migrate a :payment_originator_type and :payment_originator_id to the payments table. The associations you defined above will automatically fill these in for you.

In Ruby on Rails, how can a model "has_many" and "belong_to" using a different field other than primary ID?

I am trying building a simple project from scratch using Rails 3. Usually, the models are like:
class Student < ActiveRecord::Base
has_many :awards
end
class Award < ActiveRecord::Base
belongs_to :student
end
and we use the award.id and student.id to get the corresponding records.
But what if it is
class Company < ActiveRecord::Base
has_many :stock_quotes
end
class StockQuote < ActiveRecord::Base
belong_to :company
end
In this case, we can use the symbol of the company, such as MSFT or GOOG to identify the company, instead of using the company.id. For example, in stock_quotes, we can store the symbol of the company directly instead of using company.id. In this case, is there a way to specify it in the models?
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many
Check out :primary_key and :foreign_key options
In addition to Slawosz' answer, this question about non-integer primary keys is also relevant to your question. IMHO, it would be easier to just use integer id's like in the example of Award and Student.
Slawosz has the answer right. To be verbose (and for the next time I search this) it should be like this:
#company
has_many :stock_quotes, :primary_key => :symbol
#stock_quote
belongs_to :company, :foreign_key => :symbol

Ruby on rails model with multiple parents

In my Rails application, I have two models, Articles and Projects, which are both associated with a user. I want to add comments to each of these models. What's the best way to structure this?
Here's my current setup:
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :project
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class User < ActiveRecord::Base
has_many :articles
has_many :projects
has_many :comments, :through => :articles
has_many :comments, :through => :projects
end
Is this the right way to handle the structure? If so, how do I manage the CommentsController to have an article_id if it was created through an Article, and a project_id if it was created through a Project? Are there special routes I should set up?
One final comment: Comments don't always have to have a user. Since this if for my website, I want anonymous viewers to be able to leave comments. Is this a trivial task to handle?
Make Comment a polymorphic model. Then create a polymorphic association.
Here's an example of polymorphic relationship from the Rails wiki and here's a Railscast from the screencast-men Ryan Bates.
You can check out - acts_as_commentable plugin http://github.com/jackdempsey/acts_as_commentable/tree/master
Or you can proceed with polymorphic relation
You could have ArticleComments and ProjectComments with similar structure but stored separately, then create a method that returns both types of comments.

Resources