I have 3 associated models:
class Brand < ActiveRecord::Base
has_many :car_models
end
class CarModel < ActiveRecord::Base
has_many :production_years
belongs_to :brand
end
class ProductionYear < ActiveRecord::Base
belongs_to :car_model
end
So, how i can make custom filter in ActiveAdmin production_year section, if i want make filtering by Brand? Default filters there: car_model select and year value
Did you try something like this?
ActiveAdmin.register ProductionYear do
filter :brand, :as => :check_boxes, :collection => proc { Brand.all }
end
EDIT oops I didn't notice the complexity of your association, I think if you add this to your ProductionYear class things should work better:
class ProductionYear < ActiveRecord::Base
belongs_to :car_model
has_one :brand, :through => :car_model
end
Related
My data model is fellows, the note and hashtag's relationship is many to many
class Note < ActiveRecord::Base
attr_accessible :title
attr_accessible :content
attr_accessible :created_at
default_scope -> { order(created_at: :desc) }
has_and_belongs_to_many :hashtags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :notes
end
class NoteHashtag < ActiveRecord::Base
t.belongs_to :note, index: true
t.belongs_to :hashtag, index: true
end
I want to inquire the sql like:
select Note.* from Note inner join NoteHashtag on Note.id=NoteHashtag.note inner join Hashtag on NoteHastag.hashtag=Hashtag.id where Hashtag.name="test"
How to convert the sql into the datamodel operation in ruby on rails4?
I try to use the:
#note=Note.joins(:hashtag).where(name: "test")
and the error is:
ActionView::Template::Error (Association named 'hashtag' was not found on Note;perhaps you misspelled it?):
You need has_many :through associations if you are going to explicitly define the join model NoteHashtag. If you delete that model, you can do #notes=Note.joins(:hashtag).where(name: "test") and ActiveRecord will generate the query you are expecting.
Otherwise you could do
class Note < ActiveRecord::Base
...
has_many :note_hashtags
has_many :hashtags, through: :note_hash_tags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
end
class NoteHashtag < ActiveRecord::Base
belongs_to :note
end
Then #notes = Note.joins(:note_hashtags).joins(:hash_tags).where(name: "test) would work.
Note that both of these will return more than one note.
You can get records from many to many relationship by doing this:-
#note = Note.joins(:hashtags).where('hashtags.name' => 'test')
Why don't you address this relationship originating from the proper model? Define your models like this:
class Note < ActiveRecord::Base
...
has_many :note_hashtags
has_many :hashtags, through: :note_hashtags
end
class Hashtag < ActiveRecord::Base
attr_accessible :name
has_many :note_hashtags
has_many :notes through: :notes_hashtags
end
class NoteHashtag < ActiveRecord::Base
belongs_to :note
belongs_to :hashtag
end
And then query like this:
Hashtag.where(name: 'Test').notes
I have a polymorphic association like so (adapted from guides.rubyonrails.com):
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
has_many :employees
end
Is there a way to get all of the possible :imageable_types only given the Picture model?
For example to get the class of has_many :quotes in the Product model, you would do:
Product.reflect_on_association(:employees).klass
to get: # => Employee
Now I want to do something similar:
Picture.reflect_on_association(:imageable).klass
This obviously throws an exception, but I want to get something like: # => [Employee, Product]
Is there a way to do this? (Without trying out all models to see if they contain has_many :pictures)
I couldn't find a way to do this without looking at all the models, so I just adapted this solution: https://stackoverflow.com/a/2315469/1440599
Is there a proper way to write this, or am I approaching this wrong? I need to do a nested include. I found This Link but it doesn't seem to work.
def show
#showring = Ring.includes(:stones => :upcharges, :variations).find(params[:id])
end
I have 3 tables...
Rings which has_many stones
Stones which has_many upcharges
Models:
class Ring < ActiveRecord::Base
has_many :stones
end
class Stone < ActiveRecord::Base
has_many :upcharges
belongs_to :ring
end
class Upcharge < ActiveRecord::Base
belongs_to :stone
end
def show
#showring = Ring.includes([{:stones => :upcharges}, :variations]).find(params[:id])
end
Added some brackets :)
Getting all upcharges :
#showring.stones.each do |s|
s.upcharges #Do whatever you need with it
end
Option 2 : Declare a has_many :through
class Ring < ActiveRecord::Base
has_many :stones
has_many :upcharges, :through => :stones
end
Then in the view :
<%= #showring.upcharges.to_json.html_safe %>
What's the best way to do this? I want to be able to give Bands and Artists genres through polymorphism. I can do it with habtm and has_many :through but I'm trying to figure out if it's possible through polymorphism.
GenreList would be a lookup table with a list of different genres (e.g. Punk, Pop, Metal). I've reviewed Ryan Bate's screencast for Polymorphic Assoiciations but I'm still stuck. Specifically, I'm not sure how to create the polymorphic table Genre which would be fed canned genres from the GenreList model (the lookup table).
Is the following correct?
rails generate model Genre genre_list_id:integer genreable_id:integer genreable_type:string
class Artist < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Band < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Genre < ActiveRecord::Base
belongs_to :genreable, :polymorphic => true
end
class GenreList < ActiveRecord::Base
end
I think your implementation is a little bit weird. The way I would do it is to create a model Genre (which it will hold all the available genres Punk, Rock, Metal etc). Then I would do all this that you've already done but without the GenreList model:
rails g model Genre genreable_id:integer genreable_type:string genre_name:string
class Artist < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Band < ActiveRecord::Base
has_many :genres, :as => :genreable
end
class Genre < ActiveRecord::Base
belongs_to :genreable, :polymorphic => true
end
Then I would do make some nested resources in my routes as:
resources :artists do
resources :genres
end
resources :bands do
resources :genres
end
and then edit my controller to handle this nested relation. With this approach say if i want to see all the genres of the first artist I would visit:
/artists/1/genres
same holds for bands. I hope that I understood your problem. Let me know if I helped!
Ok, after 6.5 hrs, I managed to figure this out. I used the inherited_resources gem to help with the controllers. To recap, I wanted to be able to add Genres to Artists and Bands through a polymorphic relationship, i.e. Genres would be a lookup table, and Genreings would be a polymorphic model that contains genres for Artists and Bands. Below is the code that worked for me:
# Generate some scaffolding
rails generate scaffold Artist name:string
rails generate scaffold Band name:string
rails generate scaffold Genre name:string
rails generate scaffold Genreing genre_id:integer genreable_id:integer genreable_type:string
# Models
class Artist < ActiveRecord::Base
has_many :genreings, :as => :genreable
has_many :genres, :through => :genreings
end
class Band < ActiveRecord::Base
has_many :genreings, :as => :genreable
has_many :genres, :through => :genreings
end
class Genre < ActiveRecord::Base
attr_accessible :name
has_many :genreings
end
class Genreing < ActiveRecord::Base
attr_accessible :genre, :genre_id, :genreable, :genreable_type, :genreable_id
belongs_to :genre
belongs_to :genreable, :polymorphic => true
end
# Controller
class GenreingsController < InheritedResources::Base
belongs_to :genreable, :polymorphic => true
end
# Artist Form View
= simple_form_for(#artist) do |f|
.inputs
= f.input :name
= f.association :genres, :as => :check_boxes
.actions
= f.button :submit
# Band Form View
... (Similar to Artist)
It is correct. One thing that seems to be missing is the has_many relationship from GenreList to Genre
class GenreList < ActiveRecord::Base
has_many :genres
end
Say I have these models
class Project < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
class User < ActiveRecord::Base
has_many :comments
end
So that I can do
p = Project.find(1, :include => :comments)
p.comments.collect(&:user).collect(&:name) # this executes select for each user
How do I say I want to also include comment's user?
I believe :include => {:comments => :user} should work.