Why does this before_save not update the verified date in rails? - ruby-on-rails

My before_save doesn't update the verified_date field.
Why is that? Other processes can update the field ok.
Model:
class Link < ActiveRecord::Base
belongs_to :group
validates_presence_of :url_address
validates_presence_of :group_id
validates_size_of :version_number, :maximum => 10 #, :allow_nil => true
before_save :verify_this_link
acts_as_list
...
def verify_this_link
verified_date = Time.now
end
end
mysql> describe links;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| url_address | varchar(255) | NO | | NULL | |
| alt_text | varchar(255) | YES | | NULL | |
| group_id | int(11) | YES | | NULL | |
| position | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| version_number | varchar(255) | YES | | NULL | |
| content_date | date | YES | | NULL | |
| verified_date | date | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
mysql> select id, substr(url_address,1,20),
verified_date from links where id > 350;
+-----+--------------------------+---------------+
| id | substr(url_address,1,20) | verified_date |
+-----+--------------------------+---------------+
| 351 | http://magicmodels.r | NULL |
| 352 | http://jsbin.com/#ja | 2014-07-12 |
| 353 | http://www.javascrip | 2014-07-12 |
| 354 | http://www.test.com | 2014-08-08 |
| 357 | http://www.t5.com | 2014-07-12 |
+-----+--------------------------+---------------+
5 rows in set (0.00 sec)

Try:
def verify_this_link
self.verified_date = Time.now
end
Reference https://stackoverflow.com/a/6326323/252671

Related

Symfony 3 & Doctrine - Complex form with OnetoMany and ManyToMany relation

I want to manage the rates of a product in multi-currency and keep historic of currency rates.
So onetomany relation:
A Rates can have many CurrencyRate.
A manyto many relation:
Many currencyRate have many currencies.
RATES :
+-----------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | YES | | NULL | |
| timeStamp | datetime | NO | | NULL | |
| currencyRate_id | int(11) | YES | MUL | NULL | |
+-----------------+----------+------+-----+---------+----------------+
CURRENCY RATES
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | NO | | NULL | |
| timeStamp | datetime | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
currencyrateshascurrencies (manytomany join table)
+-----------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| currency_id | int(11) | NO | PRI | NULL | |
| currencyRate_id | int(11) | NO | PRI | NULL | |
+-----------------+---------+------+-----+---------+-------+
currencies
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| abreviation | varchar(5) | NO | UNI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
I want to generate a form from all of this.
The html form would get all available currencies with a text field to indicate the CurrencyRate.
Ex :
USD <input type="text">
EUR <input type="text">
CNY <input type="text">
...
I saw the documentation on Symfony about manytomany form. But mine is more complex with an additional onetomany relation and text field. I am totaly lost.
Thanks if you can put me on the right direction.
Best regards,
Pierre
I found the response :
First I needed to correct my Schema which didn't need a ManytoMany relation :
Rates :
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | YES | | NULL | |
| timeStamp | datetime | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
CURRENCY RATES
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ratio | double | NO | | NULL | |
| currency_id | int(11) | YES | MUL | NULL | |
| Rate_id | int(11) | YES | MUL | NULL | |
+-------------+---------+------+-----+---------+----------------+
Currencies
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| abreviation | varchar(5) | NO | UNI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Then One Form Type
class CurrencyRateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('currency',EntityType::class, array(
'class'=>'AppBundle:Currencies',
'choice_label' => 'abreviation',
'disabled' => true,
))
->add('ratio', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => CurrencyRates::class,
));
}
}
which is embedded in the final form type :
class RateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder ->add('rate')
->add('CurrencyRate', CollectionType::class, array(
'entry_type' => CurrencyRateType::class,
'entry_options' => array('label' => false))
)
->add('save', SubmitType::class, array('label' => 'create'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Rates::class,
));
}
}

Querying in Rails

How can I get the first name and last name from the table User through table Friend which is referenced by the user_id with the condition of user_id 5 and status is pending and place it in a each loop but i also need the contents of friend controller in rails. Thank a lot for the help
Controller
#add = Friend.where(user_id: 5, status: 'Pending')
User Database
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| first_name | text | YES | | NULL | |
| last_name | text | YES | | NULL | |
| email | text | YES | | NULL | |
| contact_number | varchar | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
Friend
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| request_date | date | YES | | NULL | |
| reason | text | YES | | NULL | |
| status | varchar(255) | YES | | NULL | |
| userid | int | YES | MUL | NULL | |
How about this:
#add = Friend.joins(:user).select("users.first_name, users.last_name").where(user_id: 5, status: 'Pending')
Seems like youre already fetching the id of the user, stored in the user_id field. In order to get the associated user, i would do
#user = User.find(#add.id)
This retrieves the user based on the id you pass, and from there you can do stuff like
#user.first_name
to get the values
All you need to do in Friend model belongs_to user and then
#add =Friend.where("user_id = ? AND status = ?", 5,"Pending")
and
<#add.each do |s|>
<%s.user.first_name%>
<%=<%s.user.last_name%>
// and so on..
<%end%>

"Extract" intervals from series in Google Sheets

If I in Google Sheets have a series defined as
[29060, 29062, 29331, 29332, 29333, 29334, 29335, 29336, 29337, 29338, 29339, 29340, 29341,
29342, 29372, 29373].
How do I make them line up in intervals like this?
|To |From |
|29060 |29062 |
|29331 |29342 |
|29372 |29373 |
I can't find any good answers for this anywhere. Please, help!
Data/Formulas
A1:
29060, 29062, 29331, 29332, 29333, 29334, 29335, 29336, 29337, 29338, 29339, 29340, 29341,
29342, 29372, 29373
B1: =transpose(split(A1,",")). Converts the input text is an a vertical array.
C1: =FILTER(B1:B16,mod(ROW(B1:B16),2)<>0). Returns values in odd rows.
D1: =FILTER(B1:B16,mod(ROW(B1:B16),2)=0). Returns values in even rows.
E1: =ArrayFormula(FILTER(C1:C8,{TRUE();C2:C8<>D1:D7+1})). Returns values that start a range.
F1: =ArrayFormula(FILTER(D1:D8,{D1:D7+2<>D2:D8;TRUE()})). Returns values that end a range.
Result
Note: A1 values are not shown for readability.
+----+---+-------+-------+-------+-------+-------+
| | A | B | C | D | E | F |
+----+---+-------+-------+-------+-------+-------+
| 1 | | 29060 | 29060 | 29062 | 29060 | 29062 |
| 2 | | 29062 | 29331 | 29332 | 29331 | 29342 |
| 3 | | 29331 | 29333 | 29334 | 29372 | 29373 |
| 4 | | 29332 | 29335 | 29336 | | |
| 5 | | 29333 | 29337 | 29338 | | |
| 6 | | 29334 | 29339 | 29340 | | |
| 7 | | 29335 | 29341 | 29342 | | |
| 8 | | 29336 | 29372 | 29373 | | |
| 9 | | 29337 | | | | |
| 10 | | 29338 | | | | |
| 11 | | 29339 | | | | |
| 12 | | 29340 | | | | |
| 13 | | 29341 | | | | |
| 14 | | 29342 | | | | |
| 15 | | 29372 | | | | |
| 16 | | 29373 | | | | |
+----+---+-------+-------+-------+-------+-------+

list of dates associated with name

What would be a good approach to report of all the dates a name occurs in a list? Can this be done with a single array formula?
Example (column A and B are input, columns C through G are to be auto-generated):
| A | B | C | D | E | F | G |
+---------+--------+--------+---------+---------+---------+---------+
| Episode | Stars | Name | Date | Date | Date | Date |
+---------+--------+--------+---------+---------+---------+---------+
| 7/24/15 | Bart | Bart | 7/24/15 | 7/18/15 | 8/15/15 | 3/29/15 |
| 8/09/15 | Maggie | Homer | 1/10/15 | | | |
| 7/24/15 | Marge | Lisa | 7/20/15 | 6/04/15 | | |
| 7/18/15 | Bart | Maggie | 8/09/15 | | | |
| 1/10/15 | Homer | Marge | 7/24/15 | | | |
| 8/15/15 | Bart | | | | | |
| 7/20/15 | Lisa | | | | | |
| 6/04/15 | Lisa | | | | | |
| 3/29/15 | Bart | | | | | |
|^^^^^^|
| |
| |
| (o)(o)
# _)
| ,___| - Thanks Dude!
| /
/___\
/ \
I don't think this is easily possible in a single arrayformula. However, as an alternative you could try this formula in cell C2:
=SORT(UNIQUE(QUERY(FILTER(B$2:B,LEN(B$2:B)))),1,1)
Then try this formula in cell D2 and drag down:
=TRANSPOSE(QUERY(A$2:B,"select A where B='"&C2&"'"))
See this example sheet to see it working: https://goo.gl/0u41u5

Why do I see NSAutoresizingMaskLayoutConstraint when all UI Elements are set to setTranslatesAutoresizingMaskIntoConstraints = NO

I am trying to debug a UI Layout and all the elements I have added in the code are labelled with [self.element setTranslatesAutoresizingMaskIntoConstraints:NO]; The only thing that is set in XIB file is the background color of the view (one of many views in a tabbed viewController.
When I look at the NSLog I am seeing the following:
*<UIWindow:0xc352370> - AMBIGUOUS LAYOUT
| *<UILayoutContainerView:0xc3651b0>
| | *<UINavigationTransitionView:0xc355b40>
| | | *<UIViewControllerWrapperView:0xbd3e250>
| | | | *<UILayoutContainerView:0xbd3da60>
| | | | | *<UITransitionView:0xbd46ed0>
| | | | | | *<UIViewControllerWrapperView:0xc09a450>
| | | | | | | *<UIView:0xbd51f40>
| | | | | | | | *<_UILayoutGuide:0xbd51fa0> - AMBIGUOUS LAYOUT
| | | | | | | | *<_UILayoutGuide:0xbd50a10> - AMBIGUOUS LAYOUT
| | | | | | | | *<UIButton:0xc064170> - AMBIGUOUS LAYOUT
| | | | | | | | | *<UIButtonLabel:0xc09d640>
| | | | | | | | *<UILabel:0xc073990> - AMBIGUOUS LAYOUT
| | | | | | | | *<UIButton:0xc0576a0> - AMBIGUOUS LAYOUT
| | | | | | | | | *<UIButtonLabel:0xc095290>
| | | | | | | | *<UIButton:0xc096640> - AMBIGUOUS LAYOUT
| | | | | | | | | <UIButtonLabel:0xc096820>
| | | | | | | | *<UIButton:0xc098b70> - AMBIGUOUS LAYOUT
| | | | | | | | | <UIButtonLabel:0xc098cb0>
| | | | | | | | *<UIButton:0xc09a4c0> - AMBIGUOUS LAYOUT
| | | | | | | | | <UIButtonLabel:0xc09a6d0>
| | | | | | | | *<UILabel:0xc09c9d0> - AMBIGUOUS LAYOUT
| | | | | | | | *<UILabel:0xc09cc60> - AMBIGUOUS LAYOUT
| | | | | | | | *<UIButton:0xc09ce00> - AMBIGUOUS LAYOUT
| | | | | | | | | <UIButtonLabel:0xc09d010>
| | | | | | | | *<UILabel:0xc0a25f0> - AMBIGUOUS LAYOUT
| | | | | | | | *<UIButton:0xc0a2800> - AMBIGUOUS LAYOUT
| | | | | | | | | <UIButtonLabel:0xc0a2a10>
| | | | | | | | *<UILabel:0xc0a5720> - AMBIGUOUS LAYOUT
| | | | | <UITabBar:0xc356c00>
| | | | | | <_UITabBarBackgroundView:0xbe28cc0>
| | | | | | | <_UIBackdropView:0xbe29100>
| | | | | | | | <_UIBackdropEffectView:0xbe296e0>
| | | | | | | | <UIView:0xbe29780>
| | | | | | <UITabBarButton:0xbd42000>
| | | | | | | <UITabBarSwappableImageView:0xbd41050>
| | | | | | | <UITabBarButtonLabel:0xbd43320>
| | | | | | <UITabBarButton:0xbd462e0>
| | | | | | | <UITabBarSwappableImageView:0xbd45d60>
| | | | | | | <UITabBarButtonLabel:0xbd45c70>
| | | | | | <UITabBarButton:0xbd47770>
| | | | | | | <UITabBarSwappableImageView:0xbd48a90>
| | | | | | | <UITabBarButtonLabel:0xbd486c0>
| | | | | | <UITabBarButton:0xbd4c0c0>
| | | | | | | <UITabBarSwappableImageView:0xbd4c220>
| | | | | | | <UITabBarButtonLabel:0xbd4aea0>
| | | | | | <UIImageView:0xbe29ed0>
| | <UINavigationBar:0xc06c4a0>
| | | <_UINavigationBarBackground:0xc05e720>
| | | | <_UIBackdropView:0xc357d70>
| | | | | <_UIBackdropEffectView:0xc3639a0>
| | | | | <UIView:0xc355470>
| | | | <UIImageView:0xc071980>
| | | <UINavigationItemView:0xc074c80>
| | | | <UILabel:0xc083730>
| | | <_UINavigationBarBackIndicatorView:0xc36edb0>
po [
(lldb) po [0xbd51fa0 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0xbd39190>(
)
(lldb) po [0xbd50a10 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x1121f8f0>(
)
(lldb) po [0xc064170 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0xc0a14e0>(
<NSLayoutConstraint:0xc093aa0 H:|-(NSSpace(20))-[UIButton:0xc064170] (Names: '|':UIView:0xbd51f40 )>,
<NSLayoutConstraint:0xc093b30 UIButton:0xc064170.width == UIButton:0xc0576a0.width>,
<NSLayoutConstraint:0xc093be0 H:[UIButton:0xc064170]-(20)-[UILabel:0xc073990]>,
<NSLayoutConstraint:0xc093c10 UILabel:0xc073990.width == UIButton:0xc0576a0.width>,
<NSLayoutConstraint:0xc096530 H:[UILabel:0xc073990]-(20)-[UIButton:0xc0576a0]>,
<NSLayoutConstraint:0xc096590 H:[UIButton:0xc0576a0]-(NSSpace(20))-| (Names: '|':UIView:0xbd51f40 )>,
<NSAutoresizingMaskLayoutConstraint:0xc0af130 h=--& v=--& H:[UIView:0xbd51f40(768)]>
)
(lldb) po [0xc09a4c0 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0xc36a0a0>(
<NSLayoutConstraint:0xc09c9a0 H:|-(<=0)-[UIButton:0xc09a4c0] (Names: '|':UIView:0xbd51f40 )>,
<NSContentSizeLayoutConstraint:0xc0ad530 H:[UIButton:0xc09a4c0(110)] Hug:250 CompressionResistance:750>
As you can see from po commands, I am getting NSAutoresizingMasktLayoutContraints. I thought this shouldn't happen?
How can I ensure that I don't get this?
Ok.. I have figured out that the constraints did not appropriately address the relationship between each other. I have updated the code and now I can get clean constraints for all aspects, except for the main UIWindow, which I am sure is just because of the problem with the UILayoutGuide being ambiguous -
*<UIWindow:0xcc6d7f0> - AMBIGUOUS LAYOUT
| *<UILayoutContainerView:0xbd34720>
| | *<UINavigationTransitionView:0xbd4b140>
| | | *<UIViewControllerWrapperView:0xcc48400>
| | | | *<UILayoutContainerView:0xbd5ad40>
| | | | | *<UITransitionView:0xbd635c0>
| | | | | | *<UIViewControllerWrapperView:0xcca68b0>
| | | | | | | *<UIView:0xcc9ddd0>
| | | | | | | | *<_UILayoutGuide:0xcc9de30> - AMBIGUOUS LAYOUT
| | | | | | | | *<_UILayoutGuide:0xcc9e090> - AMBIGUOUS LAYOUT
| | | | | | | | *<UIButton:0xcc9d910>
| | | | | | | | | <UIImageView:0xcc9dd20>
| | | | | | | | *<UILabel:0xcc9e860>
| | | | | | | | *<UIButton:0xcc9ebb0>
| | | | | | | | | <UIImageView:0xcc9eda0>
| | | | | | | | *<UIButton:0xcc9f020>
| | | | | | | | | <UIButtonLabel:0xcc9f470>
| | | | | | | | *<UIButton:0xcca0620>
| | | | | | | | | <UIButtonLabel:0xcca0830>
| | | | | | | | *<UIButton:0xcca15d0>
| | | | | | | | | *<UIButtonLabel:0xcca17e0>
| | | | | | | | *<UILabel:0xcca2580>
| | | | | | | | *<UILabel:0xcca2710>
| | | | | | | | *<UIButton:0xcca28d0>
| | | | | | | | | *<UIButtonLabel:0xcca2ae0>
| | | | | | | | *<UILabel:0xcca3880>
| | | | | | | | *<UILabel:0xcca3bd0>
| | | | | | | | *<UIButton:0xcca3e30>
| | | | | | | | | *<UIButtonLabel:0xcca4040>
| | | | | | | | *<UILabel:0xcca4de0>
| | | | | | | | *<UILabel:0xcca5000>
| | | | | <UITabBar:0xcc69e40>
| | | | | | <_UITabBarBackgroundView:0xcccad10>
| | | | | | | <_UIBackdropView:0xcccb150>
| | | | | | | | <_UIBackdropEffectView:0xcccb730>
| | | | | | | | <UIView:0xcccb7d0>
| | | | | | <UITabBarButton:0xcc96bf0>
| | | | | | | <UITabBarSwappableImageView:0xcc8d330>
| | | | | | | <UITabBarButtonLabel:0xcc8d270>
| | | | | | <UITabBarButton:0xcc98830>
| | | | | | | <UITabBarSwappableImageView:0xcc81840>
| | | | | | | <UITabBarButtonLabel:0xcc983a0>
| | | | | | <UITabBarButton:0xcc9a050>
| | | | | | | <UITabBarSwappableImageView:0xcc8e3e0>
| | | | | | | <UITabBarButtonLabel:0xcc9a130>
| | | | | | <UITabBarButton:0xcc9a560>
| | | | | | | <UITabBarSwappableImageView:0xcc9b140>
| | | | | | | <UITabBarButtonLabel:0xcc9aa20>
| | | | | | <UIImageView:0xcccbf20>
| | <UINavigationBar:0xbd27340>
| | | <_UINavigationBarBackground:0xbd2fc60>
| | | | <_UIBackdropView:0xbd43740>
| | | | | <_UIBackdropEffectView:0xbd2c200>
| | | | | <UIView:0xbd46100>
| | | | <UIImageView:0xbd2ffd0>
| | | <UINavigationItemView:0xbd1e270>
| | | | <UILabel:0xbd283c0>
| | | <_UINavigationBarBackIndicatorView:0xbd566e0>
The UILayoutGuide is not set anywhere in my code, so I am not quite sure what do with it.

Resources