class CashOrderStatus < ActiveRecord::Base
belongs_to:cash_order
end
usually the db need a table cash_order_statuses to mapping this model,but now i want to
mapping this model to a specific sql view like
select * from order_statues where cash_order_id is not null <=> CashOrderStatus
does rails provide some way to achieve this
There are multiple ways to fulfill your requirement:
In your CashOrderStatus model, you can set table name to override default ORM mapping:
class CashOrderStatus > ActiveRecord::Base
set_table_name "order_statuses"
belongs_to:cash_order
end
You can implement STI(Single Table Inheritance) functionality where in your database table "order_statuses" one more column will be there: type which will hold the derived model class name(In this case, CashOrderStatus).
So your model will look like this:
class CashOrderStatus > OrderStatus
set_table_name "order_statuses"
belongs_to:cash_order
end
And OrderStaus model will be derived from AR::Base class. Try it.
NOTE: Sorry for the class inheritance notation. It should be < instead of >. There is formatting issue in my stackoverflow account, so I put like this :-)
Related
I have this problem. I need to use an existing table on a mysql database. The name of the table is not compatible with RoR conventions and I need to remap the table name and the name of the attributes. I have created a scaffold to visualize on a web page the content of the table but I can't change the mapping. Is there a solution to indicate to RoR the relation between the name of the class and the name of the table in the database? and a solution to indicate the relation between the attribute of the class and field on the table?
Thanks.
The table name can be specified using table_name class method.
For the attributes/column, you need to explicitly specify aliases for the attributes using alias_attribute method. For example, if you have name_of_thing column, but want to treat it as name, then you need something like this in your model:
class CreateUtenti < ActiveRecord::Base
self.table_name = "another_name"
alias_attribute :name, :name_of_thing
end
Yes you can pass table name in model like:
class YourModel < ActiveRecord::Base
self.table_name = "pass_table_name_here"
end
I am trying to create a ActiveRecord database by using Ruby on Rails
I have created a database Schema:
To create tables destinations, productions, rules I am using rails generate model Name parameter:integer
How to create a table rule_input with foreign key to rule?
Also, how to create a table or a model sources that would join all these tables and I could get source like: source = Source.find(1) and for example render json: {source: source}?
How to create a table rule_input with foreign key to rule?
Assuming you are asking for cli command - "rails generate model rule_input rule:references"
Also, how to create a table or a model sources that would join all these tables and I could get source like: source = Source.find(1) and for example render json: {source: source}?
Single table inheritance may be a possible solution here.
class Source < ActiveRecord::Base; end
class Rule < Source
has_many :rule_inputs
end
class Production < Source; end
class Destination < Source; end
class RuleInput < ActiveRecord::Base
belongs_to :rule
end
Basically single table inheritance lets different models inherit from a parent model within a single table, as long as your data structure between models are fairly similar this would be a viable option for you. (STI eliminate having 3 tables with all the same columns)
As #maxhungry mentioned you can do STI (Single Table Inheritance) to eliminate using three tables Rules, Productions and Destinations and replace it with "type" column using Source model.
Here are some good articles on STI and about refactoring such models.
https://about.futurelearn.com/blog/refactoring-rails-sti/
http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-1/
And about your question - do I need to think about model as an object or as a table?
Model is not same as Table as we can have table-less models. You may read this Rails model without a table. So if you subclass your model from ActiveRecord::Base then it will point to a Table. For example, this code
class Product < ActiveRecord::Base
end
This will create a Product model, mapped to a products table at the database. But if you just say
class Product
end
then its a table-less model.
I would like to create a list of model objects that designate a new relationship without having to use raw sql.
Suppose I have the following models:
class MealCombination < ActiveRecord::Base
belongs_to :drink
belongs_to :food
end
class Food < ActiveRecord::Base
has_many :meal_combinations
end
class Drink < ActiveRecord::Base
has_many :meal_combinations
end
I would like to create a list of MealCombination objects that do not presently exist in the database.
Say my query would look something like this:
select distinct DRINK.id, FOOD.id from FOOD, DRINK where DRINK.alchohol_volume > 5 and FOOD.spice_factor > 45;
What is the most efficient way to create the MealCombination objects from this selection?
Iterating through an array returned back from the raw sql seems inefficient. I do not want to persist the objects into the database.
The easiest way to do this would probably be to create a view called "meal_combinations" in your database which will represent your select query you want to do. ActiveRecord should treat the view as a table for the most part, but you should definitely add
def read_only?
true
end
to any class definition that uses a view. The main disadvantage to having a lot of views is that they make database migrations a little more of a pain to manage if you ever want to change columns in the underlying tables.
I have hierarchical structure for model Board (implemented using ancestry gem).
Instead of one model and some scopes, I'd like to have two models: Board for root level elements (ancestry column value is nil), and Category for the rest(ancestry column value is not nil). They would be using the same table boards.
How can I do something like this?
You can explicity define a table for a model using set_table_name or self.table_name depending on your rails version. Also you can define a default scope for every query made for this model, using default_scope, so a combination of both should be what you are searching for:
class Category < AR:Base
self.table_name = 'boards'
default_scope where('boards.ancestry IS NOT NULL')
end
You could specify the table name of the category model and generate a default scope:
class Category < ActiveRecord::Base
self.table_name = "boards"
default_scope where('boards.ancestry IS NOT NULL')
end
And you should be able to interact with both models wit the boards-Table.
Or you stay with one model and add two modules for the specific stuff. That depends on your preferences.
I'm using a tool(UltraSms) that required three tables named (smsin, smsout & smsparts)
I need these tables to be used in the same Rails application that has other tables. With ActiveRecrod I know that table names has to be plural of the Active record class name by convention. Is there a way to map those to an ActiveRecrod class easily or should I find manual way to do ORM for it?
Thanks,
Tam
Seems that in Rails3.1 , the method name changed to table_name=, e.g.
class Mouse < ActiveRecord::Base
self.table_name = "mice"
end
You can do this:
class MyClass < ActiveRecord::Base
set_table_name "smsin"
end