I have Room, Home and Work models as following:
class Home < ActiveRecord::Base
has_many :rooms, as: :available_room
end
class Work < ActiveRecord::Base
has_many :rooms, as: :available_room
end
class Room < ActiveRecord::Base
belongs_to :available_room, polymorphic: true
end
The migration of Room looks as following
class CreateRoom < ActiveRecord::Migration
def change
create_table :rooms do |t|
t.integer :area
t.references :available_room, polymorphic: true
t.timestamps
end
end
end
Now, I want to configure the Home model, using RailsAdmin, and be able to specify a number of available rooms as well as areas.
How can do it via rails_admin in the model Home?
Thanks!
class Room < ActiveRecord::Base
belongs_to :available_room, :polymorphic => true, :inverse_of => :rooms
end
class Home < ActiveRecord::Base
has_many :rooms, :as => :available_room, :inverse_of => :available_room
rails_admin do
field :rooms
end
end
Try something like this. RailsAdmin should render a pretty widget for adding new records.
Related
I'm trying to figure out how to setup the following. A user can create a review and then like his review or like other reviews if he wants to. I came up with the following setup:
class Like < ApplicationRecord
belongs_to :user
belongs_to :review
end
class User < ApplicationRecord
has_many :reviews
has_many :likes
has_many :liked_reviews, through: :likes, source: :review
end
class Review < ApplicationRecord
belongs_to :user
has_many :likes
has_many :liking_users, :through => :likes, :source => :user
end
class CreateLikes < ActiveRecord::Migration[6.0]
def change
create_table :likes do |t|
t.references :user, index: true
t.references :likes, index: true
t.integer :review_id
t.timestamps
end
end
end
I'm pretty sure that the model associations I came up with are correct, but I'm not so sure about the like table. Can someone please review the above and tell me if the model associations and like table are correct? Thanks in advance!
I'm currently working in a project in which users can add users to a company, I want to save who added each user so there would be a belongs_to relation but one user can also add multiple users so that's a has_many.
class Passenger
belongs_to :passenger, index: true
has_many :passengers
end
I don't know if I can do this
What you need is self-jons.
# app/model/passenger.rb
class Passenger < ApplicationRecord
has_many :creations, class_name: 'Passenger', foreign_key: :passenger_id
belongs_to :creator, class_name: 'Passenger', optional: true
end
You should have a migration with
class CreatePassengers < ActiveRecord::Migration[5.0]
def change
create_table :passengers do |t|
t.references :passenger
# other attributes
end
end
end
I have three models , Article, User, Approval. and this is the Approval table
def change
create_table :approvals do |t|
t.references :user, foreign_key: true
t.references :approvable, polymorphic: true
t.timestamps
end
end
this is the three models
class User < ApplicationRecord
has_many :approvals
has_many :articles, dependent: :destroy
end
class Article < ApplicationRecord
belongs_to :user
has_many :approvals, as: :approvable
end
class Approval < ApplicationRecord
belongs_to :user
belongs_to :approvable, polymorphic: true
end
now i want to show an approval image in the article show page, and it's diffenrence according if the current_user has approved the article, i think there is a very eazy way to do this, but i can only come up with a stupid way to
do this, like :
<% #article.approvals.each do |approval| %>
<% if approval.user_id == current_user.id %>
# show has approvaled image
<% end %>
<% end %>
i think it maybe inefficient, please tell me a better solution, Thanks! : )
maybe i didn't express very well, i mean the approval is praise or like?
You can first check for approvals which belongs to current_user and article.
So in controller method use(considering you have only one article at a time),
#current_user_approvals = Approval.where(user_id: current_user.id, article_id: #article.id)
and then use #current_user_approvals in view as,
<% #current_user_approvals.each do |approval| %>
# show has approval image
<% end %>
Polymorphic should not be used in this case. you can use has_many :through to achieve approval. read the docs here
the migration would be:
class CreateApprovals < ActiveRecord::Migration[5.0]
def change
create_table :approvals do |t|
t.belongs_to :user
t.belongs_to :article
end
end
end
models would be like:
class User < ApplicationRecord
has_many :articles, dependent: :destroy
has_many :approvals
has_many :approved_articles, through: :approvals, source: :article
end
class Article < ApplicationRecord
belongs_to :user
has_many :approvals
has_many :approved_users, through: :approvals, source: :user
end
class Approval < ApplicationRecord
belongs_to :user
belongs_to :article
end
Now we can do the tricks (in rails console):
# create a user
user = User.create(name: 'user')
# create an article
article = user.articles.create(name: 'article')
# check whether article is approved
user.approved_articles.include?(article) # => false
# approve the article
user.approved_articles << article
# check check whether article is approved again
user.approved_articles.include?(article) # => true
edited version, since we do want to use polymorphic on approvals. the key is to use source_type to specify the polymorphic type (in this case it's 'Article')
class User < ApplicationRecord
has_many :articles, dependent: :destroy
has_many :approvals
has_many :approved_articles, through: :approvals, source: :approvable, source_type: 'Article'
end
class Article < ApplicationRecord
belongs_to :user
has_many :approvals, as: :approvable
has_many :approved_users, through: :approvals, source: :user
end
class Approval < ApplicationRecord
belongs_to :user
belongs_to :approvable, polymorphic: true
end
tests above in console still work.
I have three Models: Deal, Zipcode, DealIncludeZipcode.
Now, the association looks like below:-
Deal Model:
class Deal < ActiveRecord::Base
has_many :deal_include_zipcodes, dependent: :destroy
has_and_belongs_to_many :zipcodes, dependent: :destroy
accepts_nested_attributes_for :deal_include_zipcodes,:reject_if => :reject_include_zipcodes, allow_destroy: true
private
def reject_include_zipcodes(attributes)
if attributes[:deal_id].blank? || attributes[:zipcode_id].blank?
if attributes[:id].present?
attributes.merge!({:_destroy => 1}) && false
else
true
end
end
end
end
class Zipcode < ActiveRecord::Base
has_and_belongs_to_many :deals
end
class DealIncludeZipcode < ActiveRecord::Base
belongs_to :deal
belongs_to :zipcode
end
Now in view I have a checkbox,on unchecking it I can select multiple zipcode to select from DealIncludeZipcode.But when I save the data it is not saving.
I have used migration for joining Zipcode and Deal Model in which my exclude zipcode functionality is working correctly.
Please provide a soloution.I have tried various method but didn't got succeed.
The whole point of has_and_belongs_to_many is that you don't have a model which joins the two parts.
class Deal < ActiveRecord::Base
has_and_belongs_to_many :zipcodes
end
class Zipcode < ActiveRecord::Base
has_and_belongs_to_many :deals
end
Would join through a "headless" table called deals_zipcodes. If you want to have a join model you need to use has_many :through instead.
class Deal < ActiveRecord::Base
has_many :deal_zipcodes, dependent: :destroy
has_many :zipcodes, through: :deal_zipcodes
end
class DealZipcode < ActiveRecord::Base
belongs_to :deal
belongs_to :zipcode
end
class Zipcode < ActiveRecord::Base
has_many :deal_zipcodes, dependent: :destroy
has_many :deals, through: :deal_zipcodes
end
I think Max's right. So your migration should be
create_table :deals do |t|
t.string :name
...
end
create_table :zipcodes do |t|
t.string :zipcode
...
end
create_table :deals_zipcodes do |t|
t.belongs_to :deal, index: true
t.belongs_to :zipcode, index: true
end
And your models should be
class Deal < ActiveRecord::Base
has_and_belongs_to_many :zipcodes
end
class Zipcode < ActiveRecord::Base
has_and_belongs_to_many :deals
end
You should probably take a look at the ActiveRecord guide, where you'll find more explanation.
I have two separate models: "page" and "user". I want to have a "comment" model that can comment on either a "page" or a "user", but not both at the same time. I want to do something like this:
class Comment < ActiveRecord::Base
belongs_to :page
belongs_to :user
end
but I'm not sure if that's the correct approach. What's the best way to handle this case?
It seems that what you need is Polymorphic Associations.
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Page < ActiveRecord::Base
has_many :comments, as: :commentable
end
And the migrations:
class CreateUsers < ActiveRecord::Migration # and similar for Pages
def change
create_table :users do |t|
...
t.references :commentable, polymorphic: true
...
end
end
end
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
...
t.integer :commentable_id
t.string :commentable_type
...
end
end
end